


Detailed explanation of jQuery's ability to dynamically add nodes and traverse nodes
This article mainly introduces jQuery's functions of dynamically adding nodes and traversing nodes. It analyzes jQuery's dynamic addition and traversal related operation techniques for page element node elements in the form of examples. Friends who need it can refer to it. I hope it can help everyone.
Dynamicly add nodes:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="jquery-1.10.2.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var link = $("<a href='http://www.baidu.com'>百度</a>"); //创建一个节点 $("p:first").append(link); //将link节点添加到第一个p中 var link = $("<a href='http://www.google.com.hk'>谷歌</a>"); //重新创建一个节点 $("p:last").append(link); //将link节点添加到最后一个p中 }) //====================================================================================== //定义一个字典风格的数组 var data = { "百度": "http://www.baidu.com", "新浪": "http://www.sina.com.cn", "搜狐": "http://www.sohu.com" }; $(function () { $.each(data, function (key, value) { //遍历这个数组,调用匿名函数进行处理 var link = $("<tr><td><a href=" + value + " rel="external nofollow" >" + key + "</a></td></tr>"); //创建一个节点 $("#table1").append(link); //将这个节点加入到table中 }) }) </script> </head> <body> <p></p> <p></p> <table id="table1"></table> </body> </html>
Traverse nodes:
HTML part:
<html> <head> <title></title> <script src="jquery-1.10.2.min.js" type="text/javascript"></script> </head> <body> <p>AA</p> <p>BB</p> <p>CC</p> <p>DD</p> <p>EE</p> <p>FF</p> <p>KK</p> </body> </html>
js part:
alert($(this).next("p").text()) //效果:当单击AA的时候会弹出BB,当点击BB的时候会弹出CC,当点击CC的时候会弹出空的警告框(因为CC这个p节点后挨着它的是p元素,所以就弹出一个空的警告框),当点击FF的时候会弹出KK,当点击KK的时候会弹出空的警告框,(因为KK这个p节点后没有同辈的p元素挨着它了,所以就弹出一个空的警告框) /*--nextAll()方法用于获取“节点之后”所有的同辈元素--*/ $("p,p").click(function () { alert($(this).nextAll().text()); //当单击p标签或者p标签的时候弹出点击的当前标签后的所有标签的text(); }) $("p,p").click(function () { alert($(this).nextAll("p").text()); //当单击p标签或者p标签的时候弹出点击的当前标签后的所有p标签的text(); }) $("p").click(function () { $(this).nextAll("p").css("background", "red"); //当点击p标签的时候将它后面的所有p标签的背景都设为红色 }) $("p").click(function () { $.each($(this).nextAll("p"), function () { $(this).css("background-color", "red") }) //当点击p标签的时候将它后面的所有p标签的背景都设为红色,与上面的那一条效果是一样的(解释:先取得当前点击的p标签后面的所有p标签,然后对它进行遍历,然后通过后面的匿名函数将取得的所有p标签的背景设为红色)注意这前后两个this意思是不一样的:前面的this指的是当前点击的p标签,后面的thi是:在获取到当前点击的p标签的“后面的p标签”后,遍历他们的每一个p,后面的thi是:在后面的匿名函数正在处理的“当前遍历到的p标签” 【前面的是当前点击的p,后面的匿名函数的真正处理的当前p】 }) $("p,p").click(function () { //$(this).css("background", "red"); $(this).siblings().css("background", "yellow"); //将当前点击的p或者P标签背景设为红色,其他的兄弟标签背景设为黄色 $(this).css("background", "red").siblings().css("background", "yellow");//与上面一句等同 }) }) </script>
Related recommendations:
Js method of dynamically adding node content to the form_javascript skills
jquery Introduction to several methods of adding nodes_jquery
PHP How to add nodes to XML_php tips
The above is the detailed content of Detailed explanation of jQuery's ability to dynamically add nodes and traverse nodes. 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





When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

Many users are increasingly favoring the electronic ecosystem of Xiaomi smart home interconnection in modern life. After connecting to the Mijia APP, you can easily control the connected devices with your mobile phone. However, many users still don’t know how to add Mijia to their homes. app, then this tutorial guide will bring you the specific connection methods and steps, hoping to help everyone in need. 1. After downloading Mijia APP, create or log in to Xiaomi account. 2. Adding method: After the new device is powered on, bring the phone close to the device and turn on the Xiaomi TV. Under normal circumstances, a connection prompt will pop up. Select "OK" to enter the device connection process. If no prompt pops up, you can also add the device manually. The method is: after entering the smart home APP, click the 1st button on the lower left

Tampermonkey Chrome extension is a user script management plug-in that improves user efficiency and browsing experience through scripts. So how does Tampermonkey add new scripts? How to delete the script? Let the editor give you the answer below! How to add a new script to Tampermonkey: 1. Take GreasyFork as an example. Open the GreasyFork web page and enter the script you want to follow. The editor here chooses one-click offline download. 2. Select a script. , after entering the script page, you can see the button to install this script. 3. Click to install this script to come to the installation interface. Just click here to install. 4. We can see the installed one-click in the installation script.
![Outlook stuck on adding account [Fixed]](https://img.php.cn/upload/article/000/887/227/171116770937641.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When you encounter problems adding accounts in Outlook, you can try the following solutions to resolve it. Typically this can be caused by a faulty network connection, corrupted user profiles, or other temporary issues. Through the methods provided in this article, you can easily solve these problems and ensure that your Outlook can run normally. Outlook stuck on adding account If your Outlook is stuck on adding account, then use these fixes mentioned below: Disconnect and reconnect the internet Temporarily disable antivirus software Create a new Outlook profile Try adding account in safe mode Disable IPv6 Run Microsoft Support and Recovery Assistant Repair Office Application Outlook Add Account Required

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

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:
