A summary of how jquery traverses the parent, child, and sibling elements of the DOM _jquery

Source: Internet
Author: User
Tags prev

The following figure shows the structure tree in HTML, through JQuery traversal, you can start from the selected (current) element, easily move up (ancestors) in the family tree, Move down (descendants), move (sibling) horizontally. This movement is referred to as traversing the DOM.

Traversal-parent (ancestor)
traverses the DOM number up.

We can get the parent element by using the following three methods:

    1. Parent ()
    2. Parents ()
    3. Parentsuntil ()

1.JQuery parent ()

The parent () method returns the immediate parent element of the selected element.
This method will only traverse the DOM tree up one level.

<section>
   
 
$ (document). Ready (function () {
  //through the parent () function, you can get the immediate parent element
  var elem = $ (' span ') of the current element. parent ();
  Console.log (Elem);
});

2.JQuery parents ()

The parents () method returns all the parent elements of the selected element.

<section>
   
 
$ (document). Ready (function () {
  //through the parent () function, you can get all the parent elements of the current element
  var Elem = $ (' span '). parents ();
  Console.log (Elem); 
});

3.JQuery Parentsuntil ()

The Parentsuntil () method returns all the parent elements between two given elements

<section>
   
 
$ (document). Ready (function () {
  //get all of the parent element
  var elem = $ (' span ') under the body label for span. Parentsuntil (' body ');
  Console.log (Elem);
});


Traversal-Child (descendant)
traverse the DOM tree down.

The following two functions can be used to traverse down:

1.children ()
2.find ()
1.JQuery Children ()

The Children () method returns all the immediate child elements of the selected element.
This method only traverses the DOM tree down the next level.

<section>
   
 
$ (document). Ready (function () {
  //Get all the direct child elements under the section label
  var elem = $ (' section '). Children ();
  Console.log (Elem);
});

2.JQuery Find ()

The Find () method returns the descendant elements of the selected element, all the way down to the last descendant.

<section>
   
 
$ (document). Ready (function () {
  //Get the child element var Elem = $ (' section ') of all P tags under the section label
  . Find (' P ');
  Console.log (elem);

  Get all child elements under the section label
  var elems = $ (' section '). Find (' * ');
  Console.log (Elems);
});

Traversal-Sibling (brother)

There are several ways to do this:
1.sibings ()
In addition to itself, iterate through all the elements of the sibling, modifying all elements of the sibling

2.next ()
In addition to itself, only the next element is modified

3.nextAll ()
In addition to itself, modify all of the following elements

4.nextUntil ()
In addition to itself, the following elements for the interval modification

5.prev ()
Modify the previous element

6.preAll ()
modify all elements that reside on the element

7.preUntil ()
interval Modification of elements located above the interval

 <style> BD *{margin:5px;
      padding:3px;
    BORDER:3PX solid black; } </style>  
  
   
 $ (document). Ready (function () {//$ ("H1"). Siblings (). css ({bord
  ER: "3px solid Red"})//$ ("H4"). Nextall (). CSS ({border: "4PX solid Grey"});
$ ("H2"). Prev (). CSS ({border: "3px solid Green"});

}); 

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.