Ancestors were fathers, grandfathers or great-grandfather, etc.
With JQuery, you can walk up the DOM tree to find the ancestors of the elements. walk up the DOM tree
These jquery methods are useful for traversing up the DOM tree: The parent () parents () Parentsuntil () jquery Parent () method
The parent () method returns the immediate parent element of the selected element.
The method will only traverse the DOM tree up one level.
The following example returns the immediate parent element for each <span> element: instance
$ (document). Ready (function () {
$ ("span"). parent ();
});
Try the jQuery parents () method yourself
The parents () method returns all the ancestor elements of the selected element, all the way up to the root element of the document (
The following example returns all ancestors of all <span> elements: instance
$ (document). Ready (function () {
$ ("span"). parents ();
});
Try it yourself.
You can also use optional parameters to filter the search for ancestor elements.
The following example returns all ancestors of all <span> elements, and it is a <ul> element: instance
$ (document). Ready (function () {
$ ("span"). Parents ("ul");
});
Try the jQuery parentsuntil () method yourself
The Parentsuntil () method returns all ancestor elements between two given elements.
The following example returns all ancestor elements between <span> and <div> elements: instance
$ (document). Ready (function () {
$ ("span"). Parentsuntil ("div");
});
Try the jQuery Traversal reference manual Yourself
For all the jquery traversal methods, please visit our jquery Traversal reference manual.
Ancestors were fathers, grandfathers or great-grandfather, etc.
With JQuery, you can walk up the DOM tree to find the ancestors of the elements. walk up the DOM tree
These jquery methods are useful for traversing up the DOM tree: The parent () parents () Parentsuntil () jquery Parent () method
The parent () method returns the immediate parent element of the selected element.
The method will only traverse the DOM tree up one level.
The following example returns the immediate parent element for each <span> element: instance
$ (document). Ready (function () {
$ ("span"). parent ();
});
Try the jQuery parents () method yourself
The parents () method returns all the ancestor elements of the selected element, all the way up to the root element of the document (
The following example returns all ancestors of all <span> elements: instance
$ (document). Ready (function () {
$ ("span"). parents ();
});
Try it yourself.
You can also use optional parameters to filter the search for ancestor elements.
The following example returns all ancestors of all <span> elements, and it is a <ul> element: instance
$ (document). Ready (function () {
$ ("span"). Parents ("ul");
});
Try the jQuery parentsuntil () method yourself
The Parentsuntil () method returns all ancestor elements between two given elements.
The following example returns all ancestor elements between <span> and <div> elements: instance
$ (document). Ready (function () {
$ ("span"). Parentsuntil ("div");
});
Try the jQuery Traversal reference manual Yourself
For all the jquery traversal methods, please visit our jquery Traversal reference manual.