Home  >  Article  >  Web Front-end  >  Example of usage of prevAll() method in jQuery

Example of usage of prevAll() method in jQuery

巴扎黑
巴扎黑Original
2017-06-24 09:57:341376browse

This article mainly introduces the usage of the prevAll() method in jQuery. The example analyzes the function, definition and usage skills of the prevAll() method to find all peer elements before the matching element set. Friends in need You can refer to the following

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

This method finds all sibling elements before the matching element set.
The collection of sibling elements can be filtered by selectors.

Grammar structure:

The code is as follows:

$("selector").prevAll(expr)

Parameter list:

Parameters Description
expr Optional. Expression used for filtering.

Example code:

Example one:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</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").prevAll().css("color","blue") 
}) 
</script>
</head>
<body>
<p class="father">
  <p>我是p元素</p>
  <span>我是span元素</span>
  <p>我是p元素</p>
  <p>我是p元素</p>
</p>
</body>
</html>

Example 2:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</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").prevAll("span").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 is the detailed content of Example of usage of prevAll() 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