Home > Web Front-end > JS Tutorial > body text

Examples of usage of prev() method in jQuery

巴扎黑
Release: 2017-06-30 14:01:53
Original
2133 people have browsed it

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)
Copy after login

Parameter list:

ParametersDescription
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>
Copy after login

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>
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!