The traversal methods are: 1. add(), used to add elements to the set of matching elements; 2. children(), used to return all direct child elements of the selected element; 3. closest() ), used to return the first ancestor element of the selected element; 4. contents(), used to return all direct child elements of the selected element; 5. each(), used to execute the function for each matching element; 7 , eq(); 8. find(); 9. first(); 10. is(); 11. last() and so on.
The operating environment of this tutorial: windows7 system, jquery3.6 version, Dell G3 computer.
jQuery traversal method summary
jQuery traversal functions include methods for filtering, finding, and concatenating elements.
Method | Description |
---|---|
add() | Adds an element to the matching element In the collection |
addBack() | Add the previous set of elements to the current collection |
andSelf() | Deprecated in version 1.8. Alias for addBack() |
Returns all direct child elements of the selected element | |
Returns the first ancestor element of the selected element | |
Returns all direct child elements of the selected element ( Contains text and comment nodes) | |
Execute the function for each matching element | |
End the latest filtering operation in the current chain, and return the set of matching elements to the previous state | |
Return with The element with the specified index number of the selected element | |
Reduce the set of matching elements to new elements matching the selector or the return value of the matching function | |
Returns the descendant elements of the selected element | |
Returns the selected element The first element | |
Returns all elements that have one or more elements inside it | |
Checks the set of matching elements based on the selector/element/jQuery object, and returns true if there is at least one matching element | |
Return the last element of the selected element | |
Pass each element in the current matching set to the function and generate a return value The new jQuery object | |
Returns the next sibling element of the selected element | ##nextAll() |
nextUntil() | |
not() | |
offsetParent( ) | |
parent() | |
parents() | |
parentsUntil() | |
prev() | |
prevAll() | |
prevUntil() | |
##siblings() | |
slice() | |
Two methods for traversing child elements
Difference: children() method Return Returns all direct child elements of the selected element (direct child elements, only look for sons and not grandchildren (: that is to say, it will not traverse recursively) find() method obtains each element in the current element collection Descendants of elements (note that the find() method must pass parameters, otherwise it will be invalid) Example: Query all child elements <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <style> div * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script> $(document).ready(function() { $("button").on("click", function() { $("ul").find("*").css({ "color": "red", "border": "2px solid red" }); }); }); </script> </head> <body class="ancestors"> <div style="width:500px;">div (父节点) <ul>ul (指定元素) <li>li (子节点1) <span>span (孙节点1)</span> </li> <li>li (子节点2) <span>span (孙节点2)</span> </li> <li>li (子节点3) <span>span (孙节点3)</span> </li> </ul> </div> <button>选取ul的所有子元素</button> </body> </html> Copy after login 7 methods of traversing sibling elements:
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> </head> <body> <div><span>Hello</span></div> <p class="selected">Hello Again</p> <p>And Again</p> <script> $("p").siblings(".selected").css("background", "yellow"); </script> </body> </html> Copy after login ##next() method <!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> </head> <body> <ul> <li>list item 1</li> <li>list item 2</li> <li class="third-item">list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul> <script> $('li.third-item').next().css('background-color', 'red'); </script> </body> </html> Copy after login nextAll() method <!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> </head> <body> <ul> <li>list item 1</li> <li>list item 2</li> <li class="third-item">list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul> <script> $('li.third-item').nextAll().css('background-color', 'red'); </script> </body> </html> Copy after login nextUntil() method <!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> <style> .siblings * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script> $(document).ready(function() { $("li.start").nextUntil("li.stop").css({ "color": "red", "border": "2px solid red" }); }); </script> </head> <body> <div style="width:500px;" class="siblings"> <ul>ul (父节点) <li>li (兄弟节点)</li> <li>li (兄弟节点)</li> <li class="start">li (类名为"start"的兄弟节点)</li> <li>li (类名为"start"的li节点的下一个兄弟节点)</li> <li>li (类名为"start"的li节点的下一个兄弟节点)</li> <li>li (类名为"start"的li节点的下一个兄弟节点)</li> <li class="stop">li (类名为"stop"的兄弟节点)</li> </ul> </div> <p>在这个例子中,我们返回在类名为“star”和类名为“stop”的 li元素之间的所有下一个兄弟元素。</p> </body> </html> Copy after login prev() method <!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> <style> .siblings * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script> $(document).ready(function() { $("li.start").prev().css({ "color": "red", "border": "2px solid red" }); }); </script> </head> <body> <div style="width:500px;" class="siblings"> <ul>ul (父节点) <li>li (兄弟节点)</li> <li>li (类名为"start"的li节点的上一个兄弟节点)</li> <li class="start">li (类名为"start"的li节点)</li> <li>li (兄弟节点)</li> <li>li (兄弟节点)</li> </ul> </div> </body> </html> Copy after login prevAll() method <!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> <style> .siblings * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script> $(document).ready(function() { $("li.start").prevAll().css({ "color": "red", "border": "2px solid red" }); }); </script> </head> <body> <div style="width:500px;" class="siblings"> <ul>ul (parent) <li>li (类名为"start"的li的上一个兄弟节点)</li> <li>li (类名为"start"的li的上一个兄弟节点)</li> <li>li (类名为"start"的li的上一个兄弟节点)</li> <li class="start">li (类名为"start"的li节点)</li> <li>li (兄弟节点)</li> <li>li (兄弟节点)</li> </ul> </div> <p>在这个例子中,我们返回类名称为“star”的li元素之前的所有兄弟元素。</p> </body> </html> Copy after login prevUntil() method <!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> <style> .siblings * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script> $(document).ready(function() { $("li.start").prevUntil("li.stop").css({ "color": "red", "border": "2px solid red" }); }); </script> </head> <body> <div style="width:500px;" class="siblings"> <ul>ul (父节点) <li class="stop">li (类名为"stop"的兄弟节点)</li> <li>li (类名为"start"的li节点的上一个兄弟节点)</li> <li>li (类名为"start"的li节点的上一个兄弟节点)</li> <li>li (类名为"start"的li节点的上一个兄弟节点)</li> <li class="start">li (类名为"start"的li节点)</li> <li>li (兄弟节点)</li> <li>li (兄弟节点)</li> </ul> </div> <p>在这个例子中,我们返回在类名为“star”和“stop”的li元素之间的所有上一个兄弟元素,。</p> </body> </html> Copy after login each() traverse the array <script> var arr = [1,3,5,7,9]; var obj = {0:1,1:3,2:5,3:7,4:9}; /** * 利用jQuery的each静态方法遍历 * 第一个参数:当前遍历到的索引 * 第二个元素:遍历到的元素 * 注意:jQuery的each方法可以遍历伪数组 */ $.each(arr,function(index,value){ console.log("jQuery-each方法遍历数组:",index,value); }) $.each(obj,function(index,value){ console.log("jQuery-each方法遍历伪数组:",index,value); }) </script> Copy after login map() traverses the array <script> var arr = [1,3,5,7,9]; var obj = {0:1,1:3,2:5,3:7,4:9}; /** *1.利用原生JS的map方法遍历 *第一个参数:遍历到的元素 *第二个参数:当前遍历到的索引 *第三个参数:当前被遍历的数组 *注意:和原生的forEach方法一样,不能遍历伪数组 */ arr.map(function(value,index,array){ console.log("原生map遍历数组:",index,value,array); }); /** obj.map(function(value,index,array){ console.log("原生map遍历伪数组:",index,value,array); //Uncaught TypeError: obj.forEach is not a function }); */ /** * 2.利用jQuery的each静态方法遍历 * 第一个参数:要遍历的数组 * 每遍历一个元素之后执行的回调函数 * 回调函数的参数: * 第一个参数:遍历到的元素 * 第二个元素:当前遍历到的索引 * 注意:和jQuery的each方法一样可以遍历伪数组 */ $.map(arr,function(value,index){ console.log("jQuery-map方法遍历数组:",index,value); }) $.map(obj,function(value,index){ console.log("jQuery-map方法遍历伪数组:",index,value); }) </script> Copy after login 1. Each
The following example is to obtain each ID value of multiple boxes; each method: Define an empty array and add ID values to the array through each method; finally After the array is converted into a string, alert this value; $(function(){ var arr = []; $(":checkbox").each(function(index){ arr.push(this.id); }); var str = arr.join(","); alert(str); }) Copy after login Execute each: checkbox return this.id; And automatically save these return values as jQuery objects, then use the get method to convert them into native Javascript arrays, then use the join method to convert them into strings, and finally alert the value; $(function(){ var str = $(":checkbox").map(function() { return this.id; }).get().join(); alert(str); }) Copy after login 4. Use each in jquery to traverse the array, using both element index and content. (i is the index, n is the content) The code is as follows:$.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n ); }); Copy after login $.each( { name: "John", lang: "JS" }, function(i, n){ alert( "Name: " + i + ", Value: " + n ); }); Copy after login <input name="aaa" type="hidden" value="111" /> <input name="bbb" type="hidden" value="222" /> <input name="ccc" type="hidden" value="333" /> <input name="ddd" type="hidden" value="444"/> Copy after login Then you use each as follows The code is as follows: $.each($("input:hidden"), function(i,val){ alert(val); //输出[object HTMLInputElement],因为它是一个表单元素。 alert(i); //输出索引为0,1,2,3 alert(val.name); //输出name的值 alert(val.value); //输出value的值 }); Copy after login 实现效果”回复”两个字只有在鼠标经过的时候才显示出来 js代码如下 实现效果,验证判断题是否都有选择 html js代码 ps:传说中attr("property", "value");在部分浏览器中不管用可以用prop,如果只是判断可以用$(this).find("ul>li>:checkbox:eq(" + i + ")").is(":checked"); 6.官方解释 概述 通用例遍方法,可用于例遍对象和数组。 参数 objectObject :需要例遍的对象或数组。 callback (可选)Function :每个成员/元素执行的回调函数。 【推荐学习:jQuery视频教程、web前端视频】 |
The above is the detailed content of What are the traversal methods in jquery?. For more information, please follow other related articles on the PHP Chinese website!