


Summary of commonly used traversal function usage examples in jQuery_jquery
The examples in this article summarize the usage of commonly used traversal functions in jQuery. Share it with everyone for your reference. The details are as follows:
1. children() function
Thechildren() function is used to select the child elements of each matching element and return it in the form of a jQuery object.
You can also use selectors to further narrow the filtering scope and filter out elements that match the specified selector.
Usage examples are as follows:
// 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id(如果有id的话) function getTagsInfo($doms){ return $doms.map(function(){ return this.tagName + (this.id ? "#" + this.id : ""); }).get(); } // 匹配id为n1的元素 var $n1 = $("#n1"); // 匹配n1的所有子元素 var $menu_li = $n1.children(); document.writeln( getTagsInfo( $menu_li ) ); // LI#n2,LI#n7,LI#n13 // 匹配n1所有含有类名active的子元素 var $active_menu_li = $n1.children(".active"); document.writeln( getTagsInfo( $active_menu_li ) ); // LI#n2 // 匹配$menu_li每个元素的所有span子元素 var $span = $menu_li.children("span"); document.writeln( getTagsInfo( $span ) ); // SPAN#n3,SPAN#n8,SPAN#n14
2. filter() function
The filter() function is used to filter out elements that match the specified expression and return it in the form of a jQuery object.
The expressions here include: selector (string), DOM element (Element), jQuery object, and function.
Usage examples are as follows:
/* $("li") 匹配n4、n5、n6这3个元素 */ //筛选出所有索引为偶数(序号为奇数)的元素,即n4、n6 document.writeln( $("li").filter( ":even" ).length ); // 2 //筛选出包含类名foo的元素,即n5 document.writeln( $("li").filter( $(".foo") ).length ); // 1 //筛选出所有带有class属性的元素,即n5、n6 document.writeln( $("li").filter( "[class]" ).length ); // 2 /* $("input") 匹配n8、n9这两个元素 */ //筛选出选中的元素,即n9 document.writeln( $("input").filter( ":checked" ).length ); // 1 var input = document.getElementsByName("codeplayer"); //筛选出所有的input元素,即n8、n9 document.writeln( $("input").filter( input ).length ); // 2 //$("div") 匹配n1、n2、n7这3个元素 //筛选出id和class属性相等的元素,即n2、n7 var $result = $("div").filter( function(index, element){ // 函数内的this === element return this.id == this.className; } ); document.writeln( $result.length ); // 2
3. not() function
Thenot() function is used to remove elements that match the specified expression from matching elements and return the retained elements in the form of a jQuery object.
The expressions here include: selector (string), DOM element (Element), jQuery object, and function.
The opposite of this function is the add() function, which is used to add elements that match the specified expression to the current matching element.
Usage examples are as follows:
/* $("li") 匹配n4、n5、n6这3个元素 */ //排除掉n6,剩下2个元素n4、n5 document.writeln( $("li").not( "#n6" ).length ); // 2 //排除掉带类名foo的元素,剩下n4、n6 document.writeln( $("li").not( $(".foo") ).length ); // 2 //排除掉所有带有class属性的元素,剩下n4 document.writeln( $("li").not( "[class]" ).length ); // 1 /* $("input") 匹配n8、n9这两个元素 */ //排除掉被选中的元素,剩下n8 document.writeln( $("input").not( ":checked" ).length ); // 1 var input = document.getElementsByTagName("input"); //排除掉所有input元素,返回空的jQuery对象 document.writeln( $("input").not( input ).length ); // 0 /* $("div") 匹配n1、n2、n7这3个元素 */ //排除掉id和class属性相等的元素,剩下n1 var $result = $("div").not( function(index, element){ // 函数内的this === element return this.id == this.className; } ); document.writeln( $result.length ); // 1
4. add() function
Theadd() function is used to add elements that match the specified expression to the current matching element and returns it in the form of a jQuery object.
The expressions here include: selector (string), HTML content (string), DOM element (Element), and jQuery object.
The opposite of this function is the not() function, which is used to remove elements that match the specified expression from the current matching elements.
Usage examples are as follows:
//返回jQuery对象所有匹配元素的标识信息数组 //每个元素形如:#id function getTagsInfo($doms){ return $doms.map(function(){ return "#" + this.id; }).get(); } //匹配所有的p元素,再加上所有的label元素 var $elements1 = $("p").add("label"); document.writeln( getTagsInfo( $elements1 ) ); // #n1,#n4,#n9,#n11 var $matches = $("span.active").add( document.getElementsByTagName("label") ); document.writeln( getTagsInfo( $matches ) ); // #n4,#n8,#n11,#n12 var $elements2 = $("label").add( $("strong") ); document.writeln( getTagsInfo( $elements2 ) ); // #n4,#n7,#n11 var $elements3 = $("span.active").add( "label", $("#n9") ); document.writeln( getTagsInfo( $elements3 ) ); // #n8,#n11,#n12 var $elements4 = $("p").add(".active").add("span:only-child"); document.writeln( getTagsInfo( $elements4 ) ); // #n1,#n3,#n6,#n7,#n8,#n9,#n12
5. slice() function
Theslice() function is used to select a continuous segment of elements in the matching element and return it in the form of a jQuery object.
This function belongs to a jQuery object (instance).
Usage examples are as follows:
// 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id(如果有id的话) function getTagsInfo($doms){ return $doms.map(function(){ return this.tagName + (this.id ? "#" + this.id : ""); }).get(); } /* $("li") 匹配n4、n5、n6、n7、n8这5个元素 */ var $li = $("li"); // 选取第2个元素 var $sub1 = $("li").slice( 1, 2); document.writeln( getTagsInfo( $sub1 ) ); // LI#n5 // 选取第4、5个元素 var $sub2 = $("li").slice( 3 ); document.writeln( getTagsInfo( $sub2 ) ); // LI#n7,LI#n8 // 选取第1~4个元素 // startIndex = length + (-5) = 0,endIndex = length + (-1) = 4 var $sub3 = $("li").slice( -5, -1); document.writeln( getTagsInfo( $sub3 ) ); // LI#n4,LI#n5,LI#n6,LI#n7
6. parent() function
Theparent() function is used to select the parent element of each matching element and returns it as a jQuery object.
You can also use selectors to further narrow the selection range and filter out elements that match the specified selector.
This function belongs to a jQuery object (instance).
Usage examples are as follows:
// 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id(如果有id的话) function getTagsInfo($doms){ return $doms.map(function(){ return this.tagName + (this.id ? "#" + this.id : ""); }).get(); } var $n2 = $("#n2"); // 获取n2的父元素 var $parents1 = $n2.parent(); document.writeln( getTagsInfo( $parents1 ) ); // DIV#n1 var $p = $("p"); // 获取所有p元素的父元素 var $parents2 = $p.parent(); document.writeln( getTagsInfo( $parents2 ) ); // DIV#n1,DIV#n5 // 获取所有p元素的包含类名"bar"的父元素 var $parents3 = $p.parent(".bar"); document.writeln( getTagsInfo( $parents3 ) ); // DIV#n5 var $foo = $(".foo"); //获取所有包含类名"foo"的元素的父元素 var $parents4 = $foo.parent(); document.writeln( getTagsInfo( $parents4 ) ); // P#n3,DIV#n5
7. parents() function
Theparents() function is used to select the ancestor elements of each matching element and return it in the form of a jQuery object.
You can also use selectors to further narrow the selection to only those elements that match the specified selector.
This function belongs to a jQuery object (instance).
Usage examples are as follows:
// 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id(如果有id的话) function getTagsInfo($doms){ return $doms.map(function(){ return this.tagName + (this.id ? "#" + this.id : ""); }).get(); } var $n4 = $("#n4"); //获取n4的祖先元素 var $parents1 = $n4.parents(); document.writeln( getTagsInfo( $parents1 ) ); // P#n3,DIV#n1,BODY,HTML var $p = $("p"); //获取所有p元素的祖先元素 var $parents2 = $p.parents(); document.writeln( getTagsInfo( $parents2 ) ); // DIV#n5,DIV#n1,BODY,HTML //获取所有p元素的包含类名"bar"的祖先元素 var $parents3 = $p.parents(".bar"); document.writeln( getTagsInfo( $parents3 ) ); // DIV#n5 var $foo = $(".foo"); //获取所有包含类名"foo"的元素的祖先元素中的div元素 var $parents4 = $foo.parents("div"); document.writeln( getTagsInfo( $parents4 ) ); // DIV#n5,DIV#n1
8. siblings() function
Thesiblings() function is used to select all sibling elements (excluding itself) of each matching element and return it in the form of a jQuery object.
You can also use selectors to further narrow the selection range and filter out elements that match the specified selector.
This function belongs to a jQuery object (instance).
Usage examples are as follows:
//返回jQuery对象所有匹配元素的标识信息数组 //每个元素形如:#id function getTagsInfo($doms){ return $doms.map(function(){ return "#" + this.id; }).get(); } var $n4 = $("#n4"); //匹配n4的所有同辈元素(同辈元素不会包括n4自己,下同) var $elements = $n4.siblings( ); document.writeln( getTagsInfo( $elements ) ); // #n2,#n5,#n7,#n8 //匹配n4所有的同辈span元素 var $matches = $n4.siblings("span"); document.writeln( getTagsInfo( $matches ) ); // #n2,#n5,#n8 var $label = $("label"); //匹配所有label元素的含有类名"active"的同辈元素 var $actives = $label.siblings(".active"); document.writeln( getTagsInfo( $actives ) ); // #n7,#n8,#n12
9. prev() and prevAll() functions
Theprev() function is used to filter the sibling elements immediately before each matching element and return it in the form of a jQuery object.
You can also use the specified selector to further narrow the filtering scope and filter out elements that match the specified selector.
The opposite of this function is the next() function, which is used to filter the sibling elements immediately after each matching element.
prevAll() function is used to select all sibling elements before each matching element and return it in the form of a jQuery object.
You can also use selectors to further narrow the selection range and filter out elements that match the specified selector.
The opposite of this function is the nextAll() function, which is used to select all sibling elements after each matching element.
Prev() usage examples are as follows:
// 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id(如果有id的话) function getTagsInfo($doms){ return $doms.map(function(){ return this.tagName + (this.id ? "#" + this.id : ""); }).get(); } //匹配所有span元素:e2、e3、e4、e5、e7、e9 var $span = $("span"); //匹配所有span元素之前紧邻的同辈元素:e3、e2、e8 //e2 => 无【没有上一个紧邻的同辈元素,因为它是同辈元素中的第一个,下同】 //e3 => 无 //e4 => e3 //e5 => e2 //e7 => 无 //e9 => e8 var $span_prev = $span.prev( ); document.writeln( getTagsInfo( $span_prev ) ); // SPAN#e3,SPAN#e2,A#e8 //匹配所有span元素之前紧邻的同辈span元素 var $span_prev_span = $span.prev( "span" ); document.writeln( getTagsInfo( $span_prev_span ) ); // SPAN#e3,SPAN#e2
PrevAll() usage examples are as follows:
//返回jQuery对象所有匹配元素的标识信息数组 //每个元素形如:#id function getTagsInfo($doms){ return $doms.map(function(){ return "#" + this.id; }).get(); } var $n6 = $("#n6"); //匹配n6之前所有的同辈元素 var $n6_prevAll = $n6.prevAll(); document.writeln( getTagsInfo( $n6_prevAll ) ); // #n5,#n4,#n2 //匹配n6之前的所有同辈strong元素 var $n6_prevAll_strong = $n6.prevAll("strong"); document.writeln( getTagsInfo( $n6_prevAll_strong ) ); // #n4 var $label = $("label"); //匹配所有label元素之前的包含类名"active"的同辈元素 var $label_prevAll_active = $label.prevAll(".active"); document.writeln( getTagsInfo( $label_prevAll_active ) ); // #n10,#n5,#n4
10. next() function and nextAll() function
Thenext() function is used to filter the sibling elements immediately after each matching element and return it in the form of a jQuery object.
You can also use the specified selector to further narrow the filtering scope and filter out elements that match the specified selector.
The opposite of this function is the prev() function, which is used to filter the sibling elements immediately before each matching element.
nextAll() function is used to select all sibling elements after each matching element and return it in the form of a jQuery object.
You can also use selectors to further narrow the selection range and filter out elements that match the specified selector.
The opposite of this function is the prevAll() function, which is used to select all sibling elements before each matching element.
next() usage examples are as follows:
// 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id(如果有id的话) function getTagsInfo($doms){ return $doms.map(function(){ return this.tagName + (this.id ? "#" + this.id : ""); }).get(); } // 匹配所有span元素:e2、e3、e4、e5、e7、e9 var $span = $("span"); // 匹配所有span元素之后紧邻的同辈元素:e5、e4、e8 // e2的下一个紧邻的同辈元素是e5 // e3的是e4 // e4没有(因为它是同辈元素中的最后一个,下同) // e5没有 // e7的是e8 // e9没有 var $span_next = $span.next( ); document.writeln( getTagsInfo( $span_next ) ); // SPAN#e5,SPAN#e4,A#e8 // 匹配所有span元素之后紧邻的同辈span元素 var $span_next_span = $span.next( "span" ); document.writeln( getTagsInfo( $span_next_span ) ); // SPAN#e5,SPAN#e4
nextAll() usage examples are as follows:
//返回jQuery对象所有匹配元素的标识信息数组 //每个元素形如:#id function getTagsInfo($doms){ return $doms.map(function(){ return "#" + this.id; }).get(); } var $n4 = $("#n4"); //匹配n4之后所有的同辈元素 var $n4_nextAll = $n4.nextAll(); document.writeln( getTagsInfo( $n4_nextAll ) ); // #n5,#n7,#n8 //匹配n4之后的所有同辈strong元素 var $n4_nextAll_strong = $n4.nextAll("strong"); document.writeln( getTagsInfo( $n4_nextAll_strong ) ); // #n7 var $label = $("label"); var $label_nextAll_active = $label.nextAll(".active"); document.writeln( getTagsInfo( $label_nextAll_active ) ); // #n7,#n8,#n12
I hope this article will be helpful to everyone’s jquery programming design.

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

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



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

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

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

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: <

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

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:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
