jQuery中prevUntil()方法用法執行個體,jqueryprevuntil
本文執行個體講述了jQuery中prevUntil()方法用法。分享給大家供大家參考。具體分析如下:
此函數尋找匹配元素之前所有的同輩元素,直到遇到expr運算式、DOM元素或者jQuery元素匹配的那個元素為止。
可以使用可選的運算式對同輩元素集合進行篩選。
注意:祖先元素中並不包含expr運算式、DOM元素或者jQuery元素匹配的元素。
文法:
文法 一:
複製代碼 代碼如下:$(selector).prevUntil(expr,filter)
參數列表:
| 參數 |
描述 |
| expr |
可選。用於篩選祖先元素的運算式。 |
| filter |
可選。用於對取得的祖先元素集合進行篩選。 |
執行個體:
執行個體一:
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="http://www.bkjia.com/" />
<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 div").prevUntil(".firstp").css("color","blue")
})
</script>
</head>
<body>
<div class="father">
<p class="firstp">我是p元素</p>
<span>我是span元素</span>
<p>我是p元素</p>
<div>我是div元素</div>
</div>
</body>
</html>
執行個體二:
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="http://www.bkjia.com/" />
<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 div").prevUntil(".firstp","span").css("color","blue")
})
</script>
</head>
<body>
<div class="father">
<p class="firstp">我是p元素</p>
<span>我是span元素</span>
<p>我是p元素</p>
<div>我是div元素</div>
</div>
</body>
</html>
文法二:
複製代碼 代碼如下:$(selector).prevUntil(element,filter)
參數列表:
| 參數 |
描述 |
| element |
可選。用於篩選祖先元素的DOM元素或者jQuery元素。 |
| filter |
可選。用於對取得的祖先元素集合進行篩選。 |
執行個體:
執行個體一:
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="http://www.bkjia.com/" />
<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 div").prevUntil(document.getElementById("firstp")).css("color","blue")
})
</script>
</head>
<body>
<div class="father">
<p id="firstp">我是p元素</p>
<span>我是span元素</span>
<p>我是p元素</p>
<div>我是div元素</div>
</div>
</body>
</html>
執行個體二:
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="http://www.bkjia.com/" />
<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 div").prevUntil(document.getElementById("firstp"),"span").css("color","blue")
})
</script>
</head>
<body>
<div class="father">
<p id="firstp">我是p元素</p>
<span>我是span元素</span>
<p>我是p元素</p>
<div>我是div元素</div>
</div>
</body>
</html>
希望本文所述對大家的jQuery程式設計有所協助。