


What is the use of jQuery traversal? Implementation of jQuery traversal
The content of this article is to introduce what is the use of jQuery traversal? Implementation of jQuery traversal. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
With jQuery traversal, you can start from the selected (current) element and easily move up (ancestors), down (descendants), and horizontally (siblings) in the family tree. This movement is called traversing the DOM. [Recommended related video tutorials: jQuery Tutorial]
1. Traverse the DOM tree upwards to find the ancestors of elements
Use: parent() method , parents() method, parentsUntil() method
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Test JQuery</title> <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(function() { //parent() 方法返回被选元素的直接父元素。该方法只会向上一级对 DOM 树进行遍历。 //parents() 方法返回被选元素的所有祖先元素,它一路向上直到文档的根元素 (<html>)。 //parentsUntil() 方法返回介于两个给定元素之间的所有祖先元素。 $("#btn_parent").click(function() { $("#myp3").parent().css({ "color": "red", "border": "3px solid red" }); }); $("#btn_parents").click(function() { $("#myp3").parents().css({ "color": "red", "border": "3px solid red" }); }); $("#btn_parentsUntil").click(function() { $("#myp3").parentsUntil("body").css({ "color": "red", "border": "3px solid red" }); }); }); </script> </head> <body> <button type="button" id="btn_parent">parent</button><br/> <button type="button" id="btn_parents">parents</button><br/> <button type="button" id="btn_parentsUntil">parentsUntil</button><br/> <p id="myp1" style="width:210px;height:90px;padding: 10px;border:2px solid blue;"> <p id="myp2" style="width:140px;height:60px;padding: 10px;border:2px solid green;"> <p id="myp3" style="width:70px;height:30px;padding: 10px;border:2px solid yellow;"> </p> </p> </p> </body> </html>
2. Traverse the DOM downwards Tree, find the descendants of elements
Use: children() method, find() method.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Test JQuery</title> <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(function() { //children() 方法返回被选元素的所有直接子元素。该方法只会向下一级对 DOM 树进行遍历。 //find() 方法返回被选元素的后代元素,一路向下直到最后一个后代。 $("#btn_children1").click(function() { $("#myp1").children().css({ "color": "red", "border": "3px solid red" }); }); $("#btn_children2").click(function() { $("#myp1").children("p.class1").css({ "color": "red", "border": "3px solid red" }); }); $("#btn_find1").click(function() { $("#myp1").find("p").css({ "color": "red", "border": "3px solid red" }); }); $("#btn_find2").click(function() { $("#myp1").find("*").css({ "color": "red", "border": "3px solid red" }); }); }); </script> </head> <body> <button type="button" id="btn_children1">children</button><br/> <button type="button" id="btn_children2">children_class1</button><br/> <button type="button" id="btn_find1">findp</button><br/> <button type="button" id="btn_find2">find*</button><br/> <p id="myp1" style="width:210px;height:140px;padding: 10px;border:2px solid blue;"> <p id="myp2" style="width:140px;height:60px;padding: 10px;border:2px solid green;"> <p id="myp3" style="width:70px;height:40px;padding: 10px;border:2px solid yellow;"> <p id="myP1" style="width:50px;height:20px;padding: 3px;border:2px solid black;"> </p> </p> </p> <p Class="class1" style="width:140px;height:30px;padding: 10px;border:2px solid green;"> </p> </p> </body> </html>
3. Traverse the sibling elements of the element:
Utilize: siblings() method, next() method, nextAll() method, nextUntil() method.
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Test JQuery</title> <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(function() { //children() 方法返回被选元素的所有直接子元素。该方法只会向下一级对 DOM 树进行遍历。 //find() 方法返回被选元素的后代元素,一路向下直到最后一个后代。 $("#btn_siblings").click(function() { $("#myp21").siblings().css({ "color": "red", "border": "3px solid red" }); }); $("#btn_next").click(function() { $("#myp21").next().css({ "color": "red", "border": "3px solid red" }); }); $("#btn_nextAll").click(function() { $("#myp21").nextAll().css({ "color": "red", "border": "3px solid red" }); }); $("#btn_nextUntil").click(function() { $("#myp21").nextUntil("h5").css({ "color": "red", "border": "3px solid red" }); }); }); </script> </head> <body> <button type="button" id="btn_siblings">siblings</button><br/> <button type="button" id="btn_next">next</button><br/> <button type="button" id="btn_nextAll">nextAll</button><br/> <button type="button" id="btn_nextUntil">nextUntil</button><br/> <p id="myp1" style="width:210px;height:190px;padding: 10px;border:2px solid blue;"> <p id="myp21" style="width:140px;height:20px;padding: 5px;border:2px solid green;"> </p> <p id="myp22" style="width:140px;height:20px;padding: 5px;border:2px solid green;"> </p> <p id="myp23" style="width:140px;height:20px;padding: 5px;border:2px solid green;"> </p> <h5>Hello</h5> <p id="myp24" style="width:140px;height:20px;padding: 5px;border:2px solid green;"> </p> </p> </body> </html>
4. Filtering method: based on its presence in a set of elements Position to select a specific element
Use: first() method, last() method, eq() method, filter() method, not() method
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Test JQuery</title> <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(function() { //first() 方法返回被选元素的首个元素。 //last() 方法返回被选元素的最后一个元素。 //eq() 方法返回被选元素中带有指定索引号的元素。 //filter() 方法允许你规定一个标准。不匹配这个标准的元素会被从集合中删除,匹配的元素会被返回。 //not() 方法返回不匹配标准的所有元素。 $("#btn_first").click(function() { $("#myp1 p").first().css({ "color": "red", "border": "3px solid red" }); }); $("#btn_last").click(function() { $("#myp1 p").last().css({ "color": "red", "border": "3px solid red" }); }); $("#btn_eq").click(function() { $("#myp1 p").eq(2).css({ "color": "red", "border": "3px solid red" }); }); $("#btn_filter").click(function() { $("#myp1 *").filter("h5").css({ "color": "red", "border": "3px solid red" }); }); $("#btn_not").click(function() { $("#myp1 *").not("h5").css({ "color": "red", "border": "3px solid red" }); }); }); </script> </head> <body> <button type="button" id="btn_first">first</button> <button type="button" id="btn_last">last</button> <button type="button" id="btn_eq">eq</button> <button type="button" id="btn_filter">filter</button> <button type="button" id="btn_not">not</button> <p id="myp1" style="width:210px;height:190px;padding: 10px;border:2px solid blue;"> <p id="myp21" style="width:140px;height:20px;padding: 5px;border:2px solid green;"> </p> <p id="myp22" style="width:140px;height:20px;padding: 5px;border:2px solid green;"> </p> <p id="myp23" style="width:140px;height:20px;padding: 5px;border:2px solid green;"> </p> <h5>Hello</h5> <p id="myp24" style="width:140px;height:20px;padding: 5px;border:2px solid green;"> </p> </p> </body> </html>
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
The above is the detailed content of What is the use of jQuery traversal? Implementation of jQuery traversal. 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











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.

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.

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.

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.

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.

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

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

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.
