jquery traverses up the DOM Tree's parents (), parent (), closest () the difference between

Source: Internet
Author: User

         This article is mainly about the jquery traversal of the DOM Tree parents (), parent (), closest () the difference between the detailed introduction, the need for a friend to come up to the reference , I hope to help you.

In this sprint, jquery is used to write the front-end UI, but jquery has parents (), parent (), closest () in the API to traverse the DOM tree up, and has been unclear about their specific differences. So take a hard look at the jquery API document, and write down the difference here for reference.    1.parents ([selector])   This method is used to select the ancestor nodes of a DOM element or DOM element set contained in a given jquery object, and to wrap those nodes back into a jquery object. The set of nodes returned is sorted in order from inside to outside.   Also, this method accepts a string selector that filters the set of child elements that match the selector from the returned set of nodes.    2.parent ([selector])   This method is used to select a DOM element or a parent node of a DOM element set that is contained in a given jquery object. Unlike parents (), it searches only one layer up, while parents () searches the entire DOM tree.   This method can also accept a string selector to filter the returned elements.   One might ask: isn't there only one parent element of a DOM element, and why should a selector selector filter it? In fact, a jquery object might contain a number of DOM elements, such as $ (' a '). Parent () is to select all the <a> tags of the parents, so return is a set of elements, so you can filter.    3.closest (selector)   This method is used to go up through the DOM elements contained in the JQuery object or the ancestor node of the DOM element set until a node conforming to the selector selector is found.   The difference between it and parents ():   closest () goes up from itself until a suitable node is found, and the jquery object that returns contains 0 or 1 objects;   parents () starts with its own parent node to traverse up, Returns all ancestor nodes and filters the nodes based on the selector, and the resulting jquery object may contain 0, 1, or more objects.     An example that can illustrate the difference:     Copy code code as follows: <! DOCTYPE html> <html> <head>   &nbsP <title>a test document</title> </head> <body>     <div>       &NBS P <p>             <span>                 & Lt;b>my parents</b>             </span>         </P&GT ;     </div> </body> </html>   in the above documents:   $ (' B '). Parents () will return: by span, p, Div, body, The jquery object constructed by elements such as HTML,   $ (' B '). Parent () will return: A jquery object constructed from span;

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.