jQuery prevAll()Method

jQuery 遍历方法jQuery Traversing Methods

Examples

Return all sibling elements that precede each <li> element with class name "start":

$(document).ready(function(){ $("li.start").prevAll().css({"color":"red","border":"2px solid red"}); });

Result:

    ul (parent)
  • li (sibling)
  • li (sibling)
  • li (sibling)
  • li (sibling with class name "start")
  • li (sibling)
  • li (sibling)

Try it Yourself »

Definition and Usage

The prevAll() method returns all sibling elements that come before the selected element.

Sibling elements are elements that share the same parent element.

DOM tree:This method traverses backwards through the sibling elements of DOM elements.

Related methods:

  • prev()- Returns the previous sibling element of the selected element
  • prevUntil()- Returns all sibling elements that precede each element between two given parameters

Syntax

$(selector).prevAll(filter)

Parameter Description
filter Optional. Specifies a selector expression that narrows down the search for sibling elements before the selected element.

Note:To return multiple sibling elements, use a comma to separate each expression.

实例

More Examples

Narrow the search scope
How to filter the search for sibling elements before an element.

Return multiple sibling elements
How to use the filter parameter to return all sibling elements of <h2> elements with class names "1", "2", and "3".

Select all sibling elements before <p> elements
How to select all sibling elements before <p> elements.

Select all sibling <p> elements before <div>
How to select all sibling <p> elements before each <div> element.


jQuery 遍历方法jQuery Traversing Methods

Other Extensions