jquery dom refers to the Document Object Model, which is a set of Web standards of the W3C International Organization. DOM can be used by JavaScript to read and change HTML, XHTML and XML documents.
Recommended: "jquery video tutorial"
What is DOM?
To change something on the page, JavaScript needs to gain access to all elements in the HTML document. This entry, along with the methods and properties for adding, moving, changing, or removing HTML elements, is obtained through the Document Object Model (DOM).
In 1998, W3C released the first version of the DOM specification. This specification allows access and manipulation of every individual element in an HTML page.
All browsers have implemented this standard, so DOM compatibility issues are almost impossible to find. DOM can be used by JavaScript to read and change HTML, -DOM attributes. The emergence of HTML-DOM is even earlier than DOM Core. It provides some more concise notations to describe the attributes of various HTML elements.
For example: Use HTML-DOM to get the form object method: document.forms
CSS-DOM
CSS-DOM is an operation for CSS. In JavaScript, the main function of CSS-DOM technology is to obtain and set various attributes of the style object. By changing various attributes of the style object, the web page can present various effects.
Method to set the font color of a style object of an element: elements.style.color = “red”;
DOM operations in JQuery
Finding nodes
Elements can read the html content in them through the text() method, which is equivalent to the innerHTML attribute of DOM
$(function(){ var $para = $("p"); // 获取<p>节点 var $li = $("ul li:eq(1)"); // 获取第二个<li>元素节点 var p_txt = $para.attr("title"); // 输出<p>元素节点属性title var ul_txt = $li.attr("title"); // 获取<ul>里的第二个<li>元素节点的属性title var li_txt = $li.text(); // 输出第二个<li>元素节点的text });
Insert nodes
Delete node:It should be noted that when deleting an element, if the current element includes child elements, they will be deleted together, and when deleting the element Will return a reference to the currently deleted element, which can be used again later.
$(function(){ var $li = $("ul li:eq(1)").remove(); // 获取第二个<li>元素节点后,将它从网页中删除。 $li.appendTo(“ul”); // 把刚才删除的又重新添加到<ul>元素里 }); //或 $(function(){ $("ul li").remove("li[title!=菠萝]"); //把<li>元素中属性title删除不等于"菠萝"的<li>元素删除 });
Clear elements:
Clear all descendant nodes in the second li in ul. Note: The difference between empty and remove is that empty clears the descendant nodes within the element, while the element itself is retained.
$(function(){ $("ul li:eq(1)").empty(); // 找到第二个<li>元素节点后,清空此元素里的内容 });
Copy node:
This copied new element does not have any behavior, that is, when you click on the cloned new element, there is no previously set click event. If necessary, you can A parameter clone(true) is passed in the clone method, which means that when copying the element, the bound events in the element are also copied.
$(function(){ $("ul li").click(function(){ $(this).clone().appendTo("ul"); // 复制当前点击的节点,并将它追加到<ul>元素 }) });
Replacement node:
$(function(){ $("p").replaceWith("<strong>你最不喜欢的水果是?</strong>"); // 同样的实现: $("<strong>你最不喜欢的水果是?</strong>").replaceAll("p"); });
Wrap node: wrap,wrapAll,wrapInner
$(function(){ $(“span”).wrap(“<strong></strong>”); })
Run result code:
<strong><span>选择你最喜欢的水果</span></strong> $("span").wrapAll("<strong></strong>");//以第一个为开始往后面紧贴 这个会破坏页面结构
Result after execution
<strong> <span>选择你最喜欢的水果</span> <span>选择你最喜欢的水果</span> </strong> <span>选择你最喜欢的水果</span> $("span").wrapInner ("<strong></strong>");
Result after execution
<span><strong>选择你最喜欢的水果</strong></span>
Attribute operation
//取值 var p_txt = $("p").attr(“title”); //设置属性 //找到a元素且有其中含有字符串“link”,修改属性href为“index.html" $(function(){ $("a:contains('link')").attr("href",“index.html"); }) //如果想同时设置多个属性可以使用一下代码 $("a:contains('link')").attr({"href":“index.html","title":"test"}); //键值对 attr({"属性1":"值1","属性2":"值2","属性3":"值3"}) //删除属性 $(“a”).removeAttr(“title”);
Note: There are many functions in jQuery that simultaneously implement value get and set, including html(), text(), height( ), width(), val(), css(), etc.
Style operation
//读取和设置样式 使用属性方式 读取样式 var p_class = $(“p”).attr(“class”); //设置样式 $(“p”).attr(“class”,”high”);
Note: Setting the style using attributes will replace the original style. If you want to achieve additional effects You can use addClass
to append styles:
Style:
<style type="text/css"> .high {font-weight:bold; color:red; } .another{font-style:italic; color:blue;} </style>
html:
<p title="选择你最喜欢的水果" class="high">选择你最喜欢的水果</p> //class="height another" 样式也可以这样写,中间用空格隔开 jQuery: $(“p”).addClass(“another”);
Note: Style settings follow two rules. If an element is added When there are multiple class values, it is equivalent to merging their styles. If different classes set the same style attribute, the latter overrides the former.
Remove style
//移除样式 $(“p”).removeClass(“high”); //同时移除多个样式 $(“p”).removeClass(“high”).removeClass(“another”); //样式全部移除 $(“p”).removeClass(); Toggle
The toggle event controls the setting and cancellation of the style. The first function block in the toggle event definition is executed when the first click is performed. The toggle event is run when the second click is performed. the second function block in the definition, and so on.
$(function(){ $(“p”).toggle(function(){ //内置方法一 添加样式 $(this).addClass(“another”); },function(){ //内置方法二 删除样式 $(this).removeClass(“another”); }) }) //会一直循环
toggleClass method has similar functions
When the hyperlink is clicked, the code is executed to set the style. At this time, it will be automatically judged when setting the style. If the current style is not on the corresponding element, the style will be added. If Removes the style from the current element.
$(function(){ $(“#link”).click(function(){ $(“p”).toggleClass(“another”); return false; }) })
Setting and getting If there is nothing in the brackets, it is taken, if there is it, it is set
--HTML文本值 //取值 var p_html = $(“p”).html(); //设置 $(“p”).html(“<strong>选择你最喜欢的水果</strong>”); --text()方法 文本 //取值 var p_text = $(“p”).text(); //设置值 $(“p”).text(“选择你最喜欢的水果”); --val()方法 value //取值 var txt_value = $(this).val(); //设置值 $(this).val("");
Traverse the node
CSS-DOM
//取值 $(“p”).css(color); //设置值 $(“p”).css(“color”,”red”); //和attr一样可以一次设置多个样式 $(“p”).css({“color”:”red”,”background”:”#003333”}); //透明度设置 $(“p”).css(“opacity”,”0.5”);
The above is the detailed content of What is jquery dom. For more information, please follow other related articles on the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.