each()
each()
method is one of the most commonly used methods for traversing elements in a jQuery object collection. It accepts a callback function as argument, which will be executed on each element. Here is an example: $("li").each(function(index) { console.log(index + ": " + $(this).text()); });
<li>
elements and output their serial numbers and text content. find()
find()
method is used to find child elements that match the selector in the current element collection. Here is an example: $("#container").find("p").css("color", "red");
<p>
elements in the #container
element to red. filter()
filter()
method is used to filter elements that meet conditions in the current element collection. Here is an example: $("li").filter(":even").css("background-color", "lightgrey");
<li>
elements and set their background color to light gray. not()
not()
method is used to remove qualified elements from the current element collection. Here is an example: $("li").not(".special").css("font-weight", "bold");
<li>
elements that do not have a special
class and make their font bold. children()
children()
method is used to select the child elements of the current element. Here is an example: $("#container").children().css("border", "1px solid black");
#container
element. siblings()
siblings()
method is used to select sibling elements of the current element. The following is an example: $("li").siblings().addClass("highlight");
highlight
class to the sibling elements of all <li>
elements to achieve the highlighting effect.
<p>The above is a detailed introduction and code examples of several commonly used jQuery traversal methods. I hope this article can help you become more proficient in using these methods to manipulate DOM elements and improve development efficiency. The above is the detailed content of Detailed explanation of jQuery traversal methods: What do you know?. For more information, please follow other related articles on the PHP Chinese website!