Home > Web Front-end > JS Tutorial > Analysis of examples using the prevUntil() method in jQuery

Analysis of examples using the prevUntil() method in jQuery

巴扎黑
Release: 2017-06-24 10:05:13
Original
1101 people have browsed it

This article mainly introduces the usage of prevUntil() method in jQuery. The example analyzes the usage skills of finding all peer elements before matching elements according to conditions. Friends in need can refer to it

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

This function searches for all sibling elements before the matching element until it encounters an exprexpression, DOM element or jQuery element that matches up to that element.
You can use optional expressions to filter the collection of sibling elements.

Note: Ancestor elements do not contain elements matching expr expressions, DOM elements or jQuery elements.

Grammar:

Grammar 1:

The code is as follows:

$(selector).prevUntil(expr,filter)
Copy after login


Parameter list:

实例:

实例一:

代码如下:

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

实例二:

代码如下:

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

语法二:

代码如下:

$(selector).prevUntil(element,filter)
Copy after login

参数列表:

##Parameter Description
exprOptional. Expression used to filter ancestor elements.
filter Optional. Used to filter the obtained collection of ancestor elements.
参数描述
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>
Copy after login

实例二:

代码如下:

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

The above is the detailed content of Analysis of examples using the prevUntil() 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