jQuery parentsUntil() method
Example
Return the between and
$(document).ready(function(){
$("span").parentsUntil("div").css({"color":"red","border":"2px solid red"});
});
Result:
body (great-great-grandparent)
div (great-grandparent)
- li (direct parent) span
Try it »
Definition and Usage
The parentsUntil() method returns all ancestor elements between the selector The difference between stop and the.
Ancestors are the parent, grandparent, great-grandparent, and so on.
**DOM Tree:** This method traverses up from the parent element along all paths of the DOM element's ancestors to the document root element, until the specified element is reached.
**Note:** If both parameters are empty, this method returns all ancestor elements (same as parents() method).
Related methods:
- parent() - returns the direct parent element of the selected element
- parents() - returns all ancestor elements of the selected element
- closest() - returns the first ancestor of the selected element
Syntax
$(
selector
).parentsUntil(
stop,filter
)
| Parameters | Description |
|---|---|
| stop | Optional. A selector expression, element, or jQuery object indicating where to stop searching for matching ancestor elements. |
| filter | Optional. Specifies a selector expression to narrow down the search for sibling elements between selector The difference between stop a selector expression for the range of ancestor elements between the two. |
**注意:**如需返回多个祖先元素,请使用逗号分隔每个表达式。 |
More examples
Narrow down the search
How to use two parameters to filter the search for the specified elements between The difference between
Return multiple ancestor elements
How to return multiple ancestor elements between the and the.
DOM
Use a DOM element to return all ancestor elements between The difference between
Use a DOM element and a selector expression to filter the search
Use a DOM element to narrow down the search for ancestor elements between The difference between
-
the range.
jQuery Traversal Methods