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:
- Parent ()
- Parents ()
- 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.
$ (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.
$ (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
$ (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.
$ (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.
$ (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"});
});