jquery Gets the parent node, child node, sibling node method

Source: Internet
Author: User

As we use parent () to get the parent node

The code is as follows Copy Code

$ (' #cur '). Parent (). parent ();
Or
$ (' #cur '). Parent (). Parent ('. pp ');
Or
$ (' #cur '). Parent ('. pp ');

The main differences between closest and parents are:

The former matches the search from the current element, the latter starting from the parent element to find
The former is searched up until a matching element is found, the latter is searched up until the root element, and then the elements are placed in a temporary set and then filtered with the given selector expression.
The former returns 0 or 1 elements, which may contain 0, 1, or more elements

Capture of the jquery sibling node

The code is as follows Copy Code

<ul>
<li>

<ul>
<li> the first </li>
<li> the second </li>
</ul>
</li>
<li>

<ul>
<li> the first </li>
<li> the second </li>
</ul>
</li></ul>

For example, to click on the H3 of a class, the UL of its brother node to add a class called "show", the code is as follows:

The code is as follows Copy Code

$ (". Title"). Click (function () {$ (this). Parent (). Find (' ul '). addclass (' Show ');};

is by first find H3 and UL common parent node Li then to use Find () found UL, there is another way is to use the siblings () function, the code is as follows:

The code is as follows Copy Code


(". Title"). Click (function () {$ (this). Siblings (' ul '). addclass (' Show ');})

How the child nodes are obtained.

The code is as follows Copy Code


$ ("#tbody1: First-child")

: First-child

Match first child element

': A ' matches only one element, and this selector matches one child element for each parent element

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.