Latest web development tutorials

jQuery prev () method

jQuery traversal methods jQuery traversal methods

Examples

Returns the previous sibling element with the class name "start" for each <li> element:

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

result:

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

try it"

Definition and Usage

prev () method returns the selected elements of the previous sibling element.

Sibling elements are elements that share the same parent element.

Note: This method returns only one element.

DOM tree: This method traverses back along the previous sibling element DOM element.

Related methods:

  • prevAll () - Returns the previous sibling of the selected element all elements
  • prevUntil () - Returns all elements of the two siblings to each element before the given parameters

grammar

$(selector).prev( filter )

参数 描述
filter 可选。规定缩小搜索前一个同级元素范围的选择器表达式。

Examples

More examples

Select each <div> element before a sibling element
How to Select the previous sibling element of each <div> element.

Narrow your search
How to Select the previous sibling <p> element for each <div> element.


jQuery traversal methods jQuery traversal methods