Home  >  Article  >  Web Front-end  >  Examples of usage of prev() method in jQuery

Examples of usage of prev() method in jQuery

巴扎黑
巴扎黑Original
2017-06-30 14:01:532167browse

This article mainly introduces the usage of the prev() method in jQuery. The example analyzes the function, definition and use of the prev() method to obtain the immediately preceding sibling element of each element in the matching element set. For tips, friends who need them can refer to

This article explains the usage of the prev() method in jQuery with examples. Share it with everyone for your reference. The specific analysis is as follows:

This method takes the immediately preceding sibling element of each element in the matching element set.
The collection of sibling elements can be filtered by selectors.

Grammar structure:

The code is as follows:

$(selector).prev(expr)

Parameter list:

Parameters Description
expr Optional. Expression used to filter the previous sibling element

##Example code:

Example 1:

The code is as follows:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.jb51.net/" /> 
<title>prev()
函数
-PHP中文网</title> 
<style type="text/css">
.father{
  height:200px;
  width:200px;
  border:1px solid blue;
}
</style>
<script type="text/
javascript
" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(
document
).ready(function(){
  $(".father p").prev().css("color","blue")
})
</script>
</head>
<body>
<p class="father">
  <p>我是p元素</p>
  <span>我是span元素</span>
  <p>我是p元素</p>
  <p>我是p元素</p>
</p>
</body>
</html>

The above code can set the class

attribute value to the font color of the previous element of each p element in the p element set under the father element Set to blue.

Example 2:

The code is as follows:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.jb51.net/" /> 
<title>prev()函数-PHP中文网</title> 
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
  $("li").prev(".js").css("color","red"); 
}) 
</script> 
</head> 
<body> 
<p> 
  <ul> 
    <li>HTML专区</li> 
    <li class="js">Javascript专区</li> 
    <li>p+Css专区</li> 
    <li>Jquery专区</li> 
  </ul> 
</p> 
</body> 
</html>

The above code can set the font color of the element whose class attribute value is js in front of each li element in the collection of all li elements. is red.

The above is the detailed content of Examples of usage of prev() method in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn