JQuery Traversal-Introduction to the method of closest () and analysis of the method of parents () _jquery

Source: Internet
Author: User

The closest () method obtains the first ancestor element of the matching selector, starting at the current element and up the DOM tree.

Grammar:

. Closest (selector)

Parameter selector is a string value that contains the selector expression for the matching element.

If you give a JQuery object that represents a collection of DOM elements, the. Closest () method allows us to retrieve these elements in the DOM tree and their ancestor elements, and construct a new jquery object with matching elements. Parents () and. Closest () methods are similar in that they are all along the The DOM tree traverses up. The difference, though subtle, is important:

. Closest () . Parents ()
Start with the current element Start from parent element
Walk up the DOM tree until you find a match that has been applied to the selector. Traverses the DOM tree up until the root element of the document, adding each ancestor element to a temporary collection, and if the selector is applied, the collection is filtered based on that selector.
Returns a JQuery object that contains 0 or one element Returns a JQuery object that contains 0, one, or more elements

Look at the following example: Demonstrates how to complete an event delegate by closest (). The yellow background is switched when the closest list element or its descendant element is clicked
Copy Code code as follows:

<! DOCTYPE html>
<script type= "Text/javascript" src= "/jquery/jquery.js" ></script>
<style>
Li {margin:3px; padding:3px; background: #EEEEEE;}
li.hilight {background:yellow;}
</style>
<body>
<ul>
<li><b>click me!</b></li>
<li>you can also <b>click me!</b></li>
</ul>
<script>
$ (document). Bind ("click", Function (e) {
$ (e.target). Closest ("Li"). Toggleclass ("Hilight");
});
</script>
</body>

1, when the mouse clicks "You can also", the color changes. This is because the closest is traversed upward from the current element. Unlike the parents () method, it is traversed from the parent element of the current element up.

2, when clicked Click Me!, the color also can change. This also goes through the steps above, matching the current element up, except that <b> does not meet the criteria and then meets <li>.
3, example demo please visit: http://www.w3school.com.cn/tiy/t.asp?f=jquery_traversing_closest

Example 2:

Copy Code code as follows:

<ul id= "One" class= "Level-1" >
<li class= "Item-i" >I</li>
<li id= "II" class= "Item-ii" >ii
<ul class= "Level-2" >
<li class= "Item-a" >A</li>
<li class= "Item-b" >b
<ul class= "Level-3" >
<li class= "Item-1" >1</li>
<li class= "Item-2" >2</li>
<li class= "Item-3" >3</li>
</ul>
</li>
<li class= "Item-c" >C</li>
</ul>
</li>
<li class= "ITEM-III" >III</li>
</ul>

Copy Code code as follows:

$ (' li.item-a '). Closest (' ul '). CSS (' background-color ', ' red ');

Results:

This changes the color of the Level-2 <ul> because it is first encountered when traversing the DOM tree up.

As shown in the following illustration:

Let's say we're searching for <li> elements.

$ (' li.item-a '). Closest (' Li '). css (' background-color ', ' red ');

This will change the color of list item A. Before traversing the DOM tree up, the. Closest () method starts searching from the LI element itself until the selector matches item A.

Example 3

We can pass the DOM element as the context (that is, to limit the maximum range of searches) in which to search for the closest element.

Copy Code code as follows:

var listitemii = document.getElementById (' II ');
$ (' li.item-a '). Closest (' ul ', listitemii). css (' background-color ', ' red ');
$ (' li.item-a '). Closest (' #one ', listitemii). css (' background-color ', ' green ');

The above code changes the color of the Level-2 <ul> because it is both the first <ul> ancestor of list item A and the descendants of list item II.

It does not change the color of the LEVEL-1 <ul> because it is not a descendant of list item II.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.