jquery gets sample code for child nodes and parent nodes _jquery

Source: Internet
Author: User
one, get child nodes

For example, is a DIV element with the test ID, so we select, $ (' #test '), we want to find a class under this div as the span element of demo, there are several ways
1, the use of filter conditions

$ (' #test Span.demo ')

2. Use the Find () function

$ (' #test '). Find (' Span.demo ')

3. Use the Children () function

$ (' #test '). Children (' Span.demo ')

Second, get the parent node
jquery gets more of the parent element methods, such as parent (), parents (), closest (), which can help you find the parent element or node
Copy Code code as follows:

<ul class= "Parent1" >
<li><a href= "#" id= "Item1" >jquery Get parent node </a></li>
<li><a href= "#" >jquery Get parent element </a></li>
</ul>

Our goal is to use the ID of item1 note A to the class for the Parent1 UL elements, there are several ways:

1.$ (' #item1 '). Parent (). Parent ('. Parent1 ');

2.$ (' li:parent ');

3.$ (' #items '). Parents ('. Parent1 ');

4.$ (' #items1 '). Closest ('. Parent1 ');

Closest first checks whether the current element matches, and returns the element itself if it matches. If it does not match, look up the parent element, one level up, until you find the element that matches the selector. If nothing is found, an empty jquery object is returned.

The main differences between closest and parents are: 1, the former from the current element to match the search, the latter from the parent element to match the search, 2, the first step up until found the matching element stopped, the latter has been looking up until the root element, and then put these elements into a temporary collection, Filtering with the given selector expression, 3, which returns 0 or 1 elements, which may contain 0, 1, or more elements. Closest is useful for handling event delegation.

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.