jQuery prevUntil()Method

jQuery 遍历方法jQuery Traversal Methods

Example

Return all sibling elements between two <li> elements with the class names "start" and "stop":

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

Result:

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

Try it Yourself »

Definition and Usage

The prevUntil() method returnsselectorandstopall sibling elements before each element in between.

Sibling elements are elements that share the same parent.

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

Note:If both parameters are empty, this method will return all sibling elements before the element (same as theprevAll()method).

Related methods:

  • prev()- Returns the previous sibling element of the selected element
  • prevAll()- Returns all sibling elements before the selected element

Syntax

$(selector).prevUntil(stop,filter)

Parameters Description
stop Optional. A selector expression, element, or jQuery object that indicates where to stop searching for matching sibling elements before the element.
filter Optional. Specifies a selector expression that narrows the search toselectorandstopthe range of sibling elements between.

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

实例

More Examples

Narrow the search range
Use two parameters to filter the search for sibling elements before each element between the two parameters.

Return multiple sibling elements
How to return multiple sibling elements between two parameters.

DOM
Use DOM elements instead of selectors to return all sibling elements between two given parameters.

Use the DOM with two parameters
instead of a selector to filter the search for sibling elements before each element between the two parameters.


jQuery 遍历方法jQuery Traversal Methods

Other Extensions