首頁  >  文章  >  web前端  >  分析使用jQuery中prevUntil()方法實例

分析使用jQuery中prevUntil()方法實例

巴扎黑
巴扎黑原創
2017-06-24 10:05:131054瀏覽

這篇文章主要介紹了jQuery中prevUntil()方法用法,實例分析了按條件查找匹配元素之前所有的同輩元素的使用技巧,需要的朋友可以參考下

本文實例講述了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.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").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" "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.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").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)

参数列表:

参数 描述
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.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").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" "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.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").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>

以上是分析使用jQuery中prevUntil()方法實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn