Latest web development tutorials

jQuery parentsUntil () method

jQuery traversal methods jQuery traversal methods

Examples

Back ancestor between all the elements <span> and <div> between:

$(document).ready(function(){
$("span").parentsUntil("div").css({"color":"red","border":"2px solid red"});
});

result:

body (great-great-grandparent)
div (great-grandparent)
    ul (grandparent)
  • li (direct parent) span

try it"

Definition and Usage

parentsUntil () method returns the ancestor between all the elements between the selector and stop.

Ancestors father, grandfather, great-grandfather, and so on.

DOM tree: This method traverses the ancestors up from the parent element DOM element, until all the path of the document root element, until it reaches the specified element up.

Note: If both arguments are null, this method will return all ancestor elements (and parents () in the same manner).

Related methods:

  • parent () - Returns the direct parent element of the selected element
  • Parents () - Returns selected elements of all ancestor element
  • Closest () - Returns selected elements of the first ancestor

grammar

$(selector).parentsUntil( stop,filter )

参数 描述
stop 可选。表示在哪里停止搜索匹配的祖先元素的选择器表达式、元素、jQuery 对象。
filter 可选。规定缩小搜索介于 selectorstop 之间的祖先元素范围的选择器表达式。

注意:如需返回多个祖先元素,请使用逗号分隔每个表达式。


Examples

More examples

Narrow your search
How to use two parameters to filter the search for specified elements between <span> and <div> between.

Return more than one ancestor
How to return between multiple ancestor <span> and <body> between.

DOM
Use DOM element returns between all ancestor elements <span> and <div> between.

Use DOM element and a selector expression to filter searches
Use DOM element to narrow your search between ancestor elements <span> and <div> between <ul> range.


jQuery traversal methods jQuery traversal methods