JQuery traversal-about closest () method and Difference Analysis with parents ()

Source: Internet
Author: User

The closest () method obtains the matching selector'sFirst Ancestor Element,Starting from the current elementAlong the DOM tree.

Syntax:

. Closest (selector)

The selector parameter is a string value that contains the selector expression of the matching element.

Given a jQuery object that represents a DOM element set, the. closest () method allows us to retrieve these elements and their ancestor elements in the DOM tree and construct a new jQuery object with matching elements .. The parents () and. closest () methods are similar. They all traverse up the DOM tree. Although the differences between the two are subtle, they are important:

. Closest () . Parents ()
Starting from the current element Starting with the parent Element
Traverse up the DOM tree until a match of the applied selector is found. Traverse up the DOM tree until the root element of the document adds Each ancestor element to a temporary set. If a selector is applied, the set is filtered Based on the selector.
Returns a jQuery object containing zero or one element. Returns a jQuery object containing zero, one, or more elements.

Let's take a look at the following example: demonstrate how to use closest () to delegate events. When clicked by the closest list element or its child element, the yellow background is switched.
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Script type = "text/javascript" src = "/jquery. js"> </script>
<Style>
Li {margin: 3px; padding: 3px; background: # EEEEEE ;}
Li. hilight {background: yellow ;}
</Style>
</Head>
<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.tar get). closest ("li"). toggleClass ("hilight ");
});
</Script>
</Body>
</Html>

1. When You click "You can also", the color changes. This is because closest traverses up from the current element. Unlike the parents () method, the parents () method traverses up from the parent element of the current element.

2. Click me! The color will also change. In the same way, after the above steps, the current element is matched up, but <B> the condition is not met, and then <li> is met.
3, instance demo please visit: http://www.w3school.com.cn/tiy/t.asp? F = jquery_traversing_closest

Example 2:
Copy codeThe Code is 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 codeThe Code is as follows:
('Li.item-a'hangzhou.closest('ul'hangzhou.css ('background-color', 'red ');

Result:

This will change the level-2 <ul> color, because this element is first encountered when the DOM tree is traversed up.

As shown in:

Suppose we search for the <li> element.

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

This changes the color of Project A in the list. Before traversing the DOM tree up, The. closest () method searches from the li element until the selector matches Project.

Example 3

We can pass the DOM element as the context (that is, to limit the maximum range of the Search) and search for the closest element.
Copy codeThe Code is as follows:
Var listItemII = document. getElementById ('II ');
$ ('Li. item-a'). closest ('U', listitemiiw.css ('background-color', 'red ');
$ ('Li. item-a'). closest ('# one', listitemiiw.css ('background-color', 'green ');

The above code will change the level-2 <ul> color, because it is both the first <ul> ancestor of list item A and the descendant of list item II.

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

Related Article

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.