- jQuery 1.4 Reference Guide
- Jonathan Chaffer Karl Swedberg Karl Swedberg
- 3126字
- 2025-02-18 07:15:24
Tree traversal methods
These methods use the structure of the DOM tree to locate a new set of elements.
.find()
Note
Get the descendants of each element in the current set of matched elements filtered by a selector.
.find(selector)
Parameters
selector
: A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .find()
method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .find()
and .children()
methods are similar, except that the latter only travels a single level down the DOM tree.
The method accepts a selector expression of the same type that we can pass to the $()
function. The elements will be filtered by testing whether they match this selector.
Consider a page with a basic nested list as follows:
<ul class="level-1"> <li class="item-i">I</li> <li class="item-ii">II <ul class="level-2"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul>
If we begin at item II
, we can find list items within it as follows:
$('li.item-ii').find('li').css('background-color', 'red');
The result of this call is a red background on items A
, B
, 1
, 2
, 3
, and C
. Even though item II
matches the selector expression, it is not included in the results; only descendants are considered candidates for the match.
As previously discussed in the section The jQuery Function, selector context is implemented with the .find()
method. Therefore, $('li.item-ii').find('li')
is equivalent to $('li', 'li.item-ii')
.
Note
Unlike in the rest of the tree traversal methods, the selector expression is required in a call to .find()
. If we need to retrieve all of the descendant elements, we can pass in the universal selector '*'
to accomplish this.
.children()
Note
Get the children of each element in the set of matched elements, optionally filtered by a selector.
.children([selector])
Parameters
selector
(optional): A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .children()
method allows us to search through the immediate children of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .find()
and .children()
methods are similar, except that the latter only travels a single level down the DOM tree.
The method optionally accepts a selector expression of the same type that we can pass to the $()
function. If the selector is supplied, the elements will be filtered by testing whether they match it.
Consider a page with a basic nested list as follows:
<ul class="level-1"> <li class="item-i">I</li> <li class="item-ii">II <ul class="level-2"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul>
If we begin at the level-2
list, we can find its children.
$('ul.level-2').children().css('background-color', 'red');
The result of this call is a red background behind the items A
, B
, and C
. As we do not supply a selector expression, all of the children are part of the returned jQuery object; if we had supplied one, only the matching items among these three would be included.
.parents()
Note
Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.
.parents([selector])
Parameters
selector
(optional): A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .parents()
method allows us to search through the ancestors of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .parents()
and .parent()
methods are similar, except that the latter only travels a single level up the DOM tree.
The method optionally accepts a selector expression of the same type that we can pass to the $()
function. If the selector is supplied, the elements will be filtered by testing whether or not they match it.
Consider a page with a basic nested list as follows:
<ul class="level-1"> <li class="item-i">I</li> <li class="item-ii">II <ul class="level-2"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul>
If we begin at item A
, we can find its ancestors as follows:
$('li.item-a').parents().css('background-color', 'red');
The result of this call is a red background for the level-2
list, the item II
, and the level-1
list (and on up the DOM tree all the way to the document's root element, which is typically <html>
). As we do not supply a selector expression, all of the ancestors are part of the returned jQuery object. If we had supplied one, only the matching items among these would be included.
.parentsUntil()
Note
Get the ancestors of each element in the current set of matched elements up to, but not including, the element matched by the selector.
.parentsUntil(selector)
Parameters
selector
: A string containing a selector expression to indicate where to stop matching ancestor elements
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .parentsUntil()
method traverses through the ancestors of these elements until it reaches an element matched by the selector passed in the method's argument. The resulting jQuery object contains all of the ancestors up to, but not including, the one matched by the .parentsUntil()
selector. Consider a page with a basic nested list as follows:
<ul class="level-1"> <li class="item-i">I</li> <li class="item-ii">II <ul class="level-2"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul>
If we begin at item A
, we can find its ancestors up to but not including <li class="level-1">
as follows:
$('li.item-a').parentsUntil('.level-1') .css('background-color', 'red');
The result of this call is a red background for the level-2
list and the item II
.
If the .parentsUntil()
selector is not matched, or if no selector is supplied, the returned jQuery object contains all of the previous jQuery object's ancestors. For example, let's say we begin at item A
again, but this time we use a selector that is not matched by any of its ancestors:
$('li.item-a').parentsUntil('.not-here') .css('background-color', 'red');
The result of this call is a red background-color
style applied to the level-2
list, the item II
, the level-1
list, the <body>
element, and the <html>
element.
.parent()
Note
Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
.parent([selector])
Parameters
selector
(optional): A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .parent()
method allows us to search through the parents of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .parents()
and .parent()
methods are similar, except that the latter only travels a single level up the DOM tree.
The method optionally accepts a selector expression of the same type that we can pass to the $()
function. If the selector is supplied, the elements will be filtered by testing whether they match it.
Consider a page with a basic nested list as follows:
<ul class="level-1"> <li class="item-i">I</li> <li class="item-ii">II <ul class="level-2"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul>
If we begin at item A
, we can find its parents.
$('li.item-a').parent().css('background-color', 'red');
The result of this call is a red background for the level-2
list. As we do not supply a selector expression, the parent element is unequivocally included as part of the object. If we had supplied one, the element would be tested for a match before it was included.
.closest()
Note
Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
.closest(selector[, context])
Parameters
selector
: A string containing a selector expression to match elements againstcontext
(optional): A DOM element within which a matching element may be found
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .closest()
method allows us to search through these elements and their ancestors in the DOM tree and construct a new jQuery object from the matching elements.
The .parents()
and .closest()
methods are similar in that they both traverse up the DOM tree. The differences between the two, though subtle, are significant:

Consider a page with a basic nested list as follows:
<ul id="one" class="level-1"> <li class="item-i">I</li> <li id="ii" class="item-ii">II <ul class="level-2"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul>
Suppose we perform a search for <ul>
elements starting at item A
.
$('li.item-a').closest('ul') .css('background-color', 'red');
This will change the color of the level-2
<ul>
, as it is the first <ul>
encountered when traveling up the DOM tree.
Suppose we search for an <li>
element instead:
$('li.item-a').closest('li') .css('background-color', 'red');
This will change the color of list item A
. The .closest()
method begins its search with the element itself before progressing up the DOM tree and stops when item A
matches the selector.
We can pass in a DOM element as the context within which to search for the closest element.
Var listItemII = document.getElementById('ii'); $('li.item-a').closest('ul', listItemII) .css('background-color', 'red'); $('li.item-a').closest('#one', listItemII) .css('background-color', 'green');
This will change the color of the level-2
<ul>
, because it is both the first <ul>
ancestor of list item A
and a descendant of list item II
. It will not change the color of the level-1
<ul>
, however, because it is not a descendant of list item II
.
.offsetParent()
Note
Get the closest ancestor element that is positioned.
.offsetParent()
Parameters
None
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .offsetParent()
method allows us to search through the ancestors of these elements in the DOM tree and construct a new jQuery object wrapped around the closest positioned ancestor. An element is said to be positioned if its CSS position
attribute is relative
, absolute
, or fixed
. This information is useful for calculating offsets for performing animations and placing objects on the page.
Consider a page with a basic nested list with a positioned element as follows:
<ul class="level-1"> <li class="item-i">I</li> <li class="item-ii" style="position: relative;">II <ul class="level-2"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul>
If we begin at item A
, we can find its positioned ancestor.
$('li.item-a').offsetParent().css('background-color', 'red');
This will change the color of list item II
, which is positioned.
.siblings()
Note
Get the siblings of each element in the set of matched elements, optionally filtered by a selector.
.siblings([selector])
Parameters
selector
(optional): A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .siblings()
method allows us to search through the siblings of these elements in the DOM tree and construct a new jQuery object from the matching elements.
The method optionally accepts a selector expression of the same type that we can pass to the $()
function. If the selector is supplied, the elements will be filtered by testing whether they match it.
Consider a page with a simple list as follows:
<ul> <li>list item 1</li> <li>list item 2</li> <li class="third-item">list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul>
If we begin at the third item
, we can find its siblings as follows:
$('li.third-item').siblings().css('background-color', 'red');
The result of this call is a red background behind items 1
, 2
, 4
, and 5
. As we do not supply a selector expression, all of the siblings are part of the object. If we had supplied one, only the matching items among these four would be included.
The original element is not included among the siblings, which is important to remember when we wish to find all of the elements at a particular level of the DOM tree.
.prev()
Note
Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.
.prev([selector])
Parameters
selector
(optional): A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .prev()
method allows us to search through the predecessors of these elements in the DOM tree and construct a new jQuery object from the matching elements.
The method optionally accepts a selector expression of the same type that we can pass to the $()
function. If the selector is supplied, the elements will be filtered by testing whether they match it.
Consider a page with a simple list as follows:
<ul> <li>list item 1</li> <li>list item 2</li> <li class="third-item">list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul>
If we begin at the third item, we can find the element that comes just before it.
$('li.third-item').prev().css('background-color', 'red');
The result of this call is a red background behind item 2
. As we do not supply a selector expression, this preceding element is unequivocally included as part of the object. If we had supplied one, the element would be tested for a match before it was included.
.prevAll()
Note
Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.
.prevAll([selector])
Parameters
selector
(optional): A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .prevAll()
method allows us to search through the predecessors of these elements in the DOM tree and construct a new jQuery object from the matching elements.
The method optionally accepts a selector expression of the same type that we can pass to the $()
function. If the selector is supplied, the elements will be filtered by testing whether they match it.
Consider a page with a simple list as follows:
<ul> <li>list item 1</li> <li>list item 2</li> <li class="third-item">list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul>
If we begin at the third item, we can find the elements that come before it.
$('li.third-item').prevAll().css('background-color', 'red');
The result of this call is a red background behind items 1
and 2
. As we do not supply a selector expression, these preceding elements are unequivocally included as part of the object. If we had supplied one, the elements would be tested for a match before they were included.
.prevUntil()
Note
Get all preceding siblings of each element up to, but not including, the element matched by the selector.
.prevUntil(selector)
Parameters
selector
: A string containing a selector expression to indicate where to stop matching previous sibling elements
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .prevUntil()
method allows us to search through the predecessors of these elements in the DOM tree, stopping when it reaches an element matched by the method's argument. The new jQuery object that is returned contains all previous siblings up to, but not including, the one matched by the .prevUntil()
selector.
If the selector is not matched or is not supplied, all previous siblings will be selected; in these cases it selects the same elements as the .prevAll()
method does when no filter selector is provided.
Consider a page with a simple definition list as follows:
<dl> <dt>term 1</dt> <dd>definition 1-a</dd> <dd>definition 1-b</dd> <dd>definition 1-c</dd> <dd>definition 1-d</dd> <dt id="term-2">term 2</dt> <dd>definition 2-a</dd> <dd>definition 2-b</dd> <dd>definition 2-c</dd> <dt>term 3</dt> <dd>definition 3-a</dd> <dd>definition 3-b</dd> </dl>
If we begin at the second term, we can find the elements that come before it until a preceding <dt>
.
$('#term-2').prevUntil('dt').css('background-color', 'red');
The result of this call is a red background behind definitions 1-a
, 1-b
, 1-c
, and 1-d
.
.next()
Note
Get the immediately following sibling of each element in the set of matched elements, optionally filtered by a selector.
.next([selector])
Parameters
selector
(optional): A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .next()
method allows us to search through the successors of these elements in the DOM tree and construct a new jQuery object from the matching elements.
The method optionally accepts a selector expression of the same type that we can pass to the $()
function. If the selector is supplied, the elements will be filtered by testing whether or not they match it.
Consider a page with a simple list as follows:
<ul> <li>list item 1</li> <li>list item 2</li> <li class="third-item">list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul>
If we begin at the third-item
, we can find the element that comes just after it as follows:
$('li.third-item').next().css('background-color', 'red');
The result of this call is a red background behind item 4
. As we do not supply a selector expression, this following element is unequivocally included as part of the object. If we had supplied one, the element would be tested for a match before it was included.
.nextAll()
Note
Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.
.nextAll([selector])
Parameters
selector
(optional): A string containing a selector expression to match elements against
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .nextAll()
method allows us to search through the successors of these elements in the DOM tree and construct a new jQuery object from the matching elements.
The method optionally accepts a selector expression of the same type that we can pass to the $()
function. If the selector is supplied, the elements will be filtered by testing whether they match it.
Consider a page with a simple list as follows:
<ul> <li>list item 1</li> <li>list item 2</li> <li class="third-item">list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul>
If we begin at the third-item
, we can find the elements that come after it.
$('li.third-item').nextAll().css('background-color', 'red');
The result of this call is a red background behind items 4
and 5
. As we do not supply a selector expression, these following elements are unequivocally included as part of the object. If we had supplied one, the elements would be tested for a match before they were included.
.nextUntil()
Note
Get all following siblings of each element up to, but not including, the element matched by the selector.
.nextUntil(selector)
Parameters
selector
: A string containing a selector expression to indicate where to stop matching following sibling elements
Return value
The new jQuery object.
Description
Given a jQuery object that represents a set of DOM elements, the .nextUntil()
method allows us to search through the successive siblings of these elements in the DOM tree, stopping when it reaches an element matched by the method's argument. The new jQuery object that is returned contains all following siblings up to, but not including, the one matched by the .nextUntil()
selector.
If the selector is not matched or is not supplied, all following siblings will be selected; in these cases it selects the same elements as the .nextAll()
method does when no filter selector is provided.
Consider a page with a simple definition list as follows:
<dl> <dt>term 1</dt> <dd>definition 1-a</dd> <dd>definition 1-b</dd> <dd>definition 1-c</dd> <dd>definition 1-d</dd> <dt id="term-2">term 2</dt> <dd>definition 2-a</dd> <dd>definition 2-b</dd> <dd>definition 2-c</dd> <dt>term 3</dt> <dd>definition 3-a</dd> <dd>definition 3-b</dd> </dl>
If we begin at the second term, we can find the elements that come after it until the next <dt>
.
$('#term-2').nextUntil('dt').css('background-color', 'red');
The result of this call is a red background behind definitions 2-a
, 2-b
, and 2-c
.