jQuery中prevUntil()方法用法实例教程
本文实例讲述了jquery中prevuntil()方法用法。分享给大家供大家参考。具体分析如下:
此函数查找匹配元素之前所有的同辈元素,直到遇到expr表达式、dom元素或者jquery元素匹配的那个元素为止。
可以使用可选的表达式对同辈元素集合进行筛选。
注意:祖先元素中并不包含expr表达式、dom元素或者jquery元素匹配的元素。
语法:
语法 一:
代码如下:
$(selector).prevuntil(expr,filter)
参数列表:
实例:
实例一:
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.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 p").prevuntil(".firstp").css("color","blue")
})
</script>
</head>
<body>
<p class="father">
<p class="firstp">我是p元素</p>
<span>我是span元素</span>
<p>我是p元素</p>
<p>我是p元素</p>
</p>
</body>
</html>
实例二:
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.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 p").prevuntil(".firstp","span").css("color","blue")
})
</script>
</head>
<body>
<p class="father">
<p class="firstp">我是p元素</p>
<span>我是span元素</span>
<p>我是p元素</p>
<p>我是p元素</p>
</p>
</body>
</html>
语法二:
代码如下:
$(selector).prevuntil(element,filter)
参数列表:
实例:
实例一:
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.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 p").prevuntil(document.getelementbyid("firstp")).css("color","blue")
})
</script>
</head>
<body>
<p class="father">
<p id="firstp">我是p元素</p>
<span>我是span元素</span>
<p>我是p元素</p>
<p>我是p元素</p>
</p>
</body>
</html>
实例二:
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.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 p").prevuntil(document.getelementbyid("firstp"),"span").css("color","blue")
})
</script>
</head>
<body>
<p class="father">
<p id="firstp">我是p元素</p>
<span>我是span元素</span>
<p>我是p元素</p>
<p>我是p元素</p>
</p>
</body>
</html>