Methods of manipulating DOM between JS and JQuery
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics





LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

Ele.me is a software that brings together a variety of different delicacies. You can choose and place an order online. The merchant will make it immediately after receiving the order. Users can bind WeChat through the software. If you want to know the specific operation method , remember to check out the PHP Chinese website. Instructions on how to bind WeChat to Ele.me: 1. First open the Ele.me software. After entering the homepage, we click [My] in the lower right corner; 2. Then in the My page, we need to click [Account] in the upper left corner; 3. Then come to the personal information page where we can bind mobile phones, WeChat, Alipay, and Taobao. Here we click [WeChat]; 4. After the final click, select the WeChat account that needs to be bound in the WeChat authorization page and click Just [Allow];

Forgetting the Win8 computer startup password is a problem that many people encounter when using computers on a daily basis. When we forget the login password, we will be unable to enter the system normally, causing inconvenience to our daily use. If you happen to encounter this problem, don’t worry. Below I will introduce some simple operations to help you quickly restore the power-on password of your Win8 computer. Method 1: Use Microsoft account password. If you use a Microsoft account to log in to your Win8 computer, you can try using the password of that account.
