Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery parents() method

jQuery parents() method

jQuery Traversal Methods jQuery Traversal Methods

Example

Return all ancestor elements of the:

$(document).ready(function(){

$("span").parents().css({"({"color":"red","border":"2px solid red"});

});

Result:

body (great-great-grandparent)

div (great-grandparent)

Try it »


Definition and Usage

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

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 ().

**Note:** If the filter parameter is empty, this method selects all ancestors in the element set along all paths from the direct parent element up to and. Therefore, passing a selector expression that narrows down the search results is very useful.

This method is similar to the closest() both traverse up the DOM tree; the differences are:

parents()

closest()

Other related methods:


Syntax

$(

selector

).parents(

filter

)

Parameters Description
filter Optional. Specifies a selector expression to narrow down the search for ancestor elements.
 **注意:**如需返回多个祖先,请使用逗号分隔每个表达式。 |

More examples

Narrow down the search
How to use the filter parameter to return among all ancestors of the