This article mainly shares with you how JS and JQuery operate DOM, mainly using code methods. I hope it can help everyone.
Query node:
js: 1. Query based on ID; 2. Query based on tag name; 3. Query based on name; 4. Query based on level; details are as follows:
<script> //1.根据ID查询节点 var ul = document.getElementById("city"); var cd = document.getElementById("cd"); console.log(ul); console.log(cd); //2.根据标签名查询节点 //2.1在整个文档(document)内查询 console.log(document.getElementsByTagName("li")); //2.2在某个元素节点(element)内查询 console.log(ul.getElementsByTagName("li")); //3.根据name查询节点(基本都是给表单控件用的) console.log(document.getElementsByName("sex")); //4.根据层次查询节点 //获取已得到的节点的父亲、孩子和兄弟 //4.1获取父亲,返回的是单个值 console.log(cd.parentNode); //4.2获取孩子,返回的是多个值 //这种方式返回的节点是个数组,并且会把空格当做孩子放入数组中 console.log(ul.childNodes); //不带空格的获取孩子的节点 console.log(ul.getElementsByTagName("li")); //标准API中没有直接查询兄弟的方法, //必须通过查询父亲、查询孩子来实现查询兄弟, //下面的语句输出:上海 console.log(cd.parentNode.getElementsByTagName("li")[1]); </script>
jQuery: Use the jQuery selector directly to select elements and perform operations; please check out another article: jQuery selector https://blog.csdn.net/huang_yx/article/details/79686975 (click to open the link)
Reading and writing nodes:
js: roughly divided into: 1. Reading and writing node names and types; 2. Reading and writing node content; 3. Reading and writing node attributes; 4. Reading and writing form controls The value of )/obj.html("123")
Read and write the text content of the node (subtags are not supported): Corresponds to point 2 of the above js
obj.text()/obj.text("123")
Read and write the attribute value of the node: corresponds to the third point of js above
obj.attr("Attribute name")/obj .val("Attribute name", "Attribute value")
Read and write the value attribute value of the node: corresponds to the 4th point of the above js
obj.val()/obj.val(" abc")
Note: obj represents jQuery object
Add and delete nodes: JS can only add and delete nodes through the parent node, but jQuery is much more convenient and has many corresponding api
js:
<script> //1.读取节点的名称和类型 //获取p1 var p1 =document.getElementById("p1"); console.log(p1.nodeName); console.log(p1.nodeType); //2.读写节点的内容(<p>内容</p>) //innerHTML:支持子标签 console.log(p1.innerHTML) console.log(p1.innerHTML = '单标签试一试') console.log(p1.innerHTML) //innerText:不支持子标签 var p2 = document.getElementById("p2"); console.log(p2.innerText); p2.innerText = "2.<u>查询</u>节点"; //3.读写节点的属性 //3.1.标准的API是下面的三个 //先取到这个节点 var img = document.getElementById("li"); console.log(img.getAttribute("src")); img.setAttribute("src", "../img/add.png"); img.removeAttribute("src"); //3.2.新的API(低版本浏览器不支持) //节点.属性名(class除外,要写成className) //注意点:.style和.className是标准的 var a = document.getElementById("baidu"); console.log(a.href); a.href = "undifined"; //4.读写表单控件的值 //input.value/input.value="" </script>
jQuert:
Create node:
$("Node content");
$("you Good")
Insert node: Commonly used API
parent.append(obj): Add it as the last child node
parent.prepend(obj ): Added as the first child node
brother.after(obj): Added as the next sibling node
brother.before(obj): Added as the previous sibling node
Delete nodes: Common API
obj.remove(): Delete nodes
obj.remove(selector): Only delete nodes that satisfy the selector
obj.empty(): Clear nodes
Traverse nodes: some APIs corresponding to jQuery to facilitate node operations
children()/children(selector): direct child nodes
next()/next(selector): The next sibling node
prev()/prev(selector): The previous sibling node
siblings()/siblings(selector): All brothers
find(selector): Find all descendants that satisfy the selector
parent(): Parent node
Summary:
JS and jQuery operations on nodes It’s nothing more than adding, deleting, modifying, and checking, but jQuery is a js framework, and its core concept is: write less, do more; it greatly simplifies code writing. It encapsulates JS, CSS, and DOM, and provides a consistent and simple API, which is more convenient and faster to use, and the corresponding writing method is also simpler.
Related recommendations:
How to operate DOM elements in js Detailed explanation of DOM event flow in jsJavaScript Optimizing DOMThe above is the detailed content of Methods of manipulating DOM between JS and JQuery. For more information, please follow other related articles on the PHP Chinese website!

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.