Home  >  Article  >  Web Front-end  >  Tutorial on using parentsUntil() method in jQuery

Tutorial on using parentsUntil() method in jQuery

巴扎黑
巴扎黑Original
2017-06-24 09:19:441385browse

This article mainly introduces the usage of the parentsUntil() method in jQuery. The example analyzes the function and definition of the parentsUntil() method and searches for all ancestors of matching elements based on conditions. For tips on using elements, friends in need can refer to

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

This method finds all ancestor elements of the matching element until it encounters the element that matches exprexpression, DOM element or jQuery element.
Obtaining the collection of ancestor elements can be filtered by filtering expressions.

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

Grammar:

Grammar 1:

The code is as follows:

$(selector).parentsUntil(expr,filter)

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>
<script type="text/
javascript
" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(
document
).ready(function(){
        $("li").parentsUntil(".inner").css("border","1px solid blue");
    })
</script>
</head>
<body>
    <p class="outer">
        <p>脚本之家欢迎您</p>
        <p class="inner">
            <ul class="first">
                <li>HTML专区</li>
                <li>Javascript专区</li>
                <li>p+CSS专区</li>
                <li>Jquery专区</li>
            </ul>
            <ul class="second">
                <li>HTML专区</li>
                <li>Javascript专区</li>
                <li>p+CSS专区</li>
                <li>Jquery专区</li>
            </ul>
        </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>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("li").parentsUntil(".inner",".second").css("border","1px solid blue");
    })
</script>
</head>
<body>
    <p class="outer">
        <p>脚本之家欢迎您</p>
        <p class="inner">
            <ul class="first">
                <li>HTML专区</li>
                <li>Javascript专区</li>
                <li>p+CSS专区</li>
                <li>Jquery专区</li>
            </ul>
            <ul class="second">
                <li>HTML专区</li>
                <li>Javascript专区</li>
                <li>p+CSS专区</li>
                <li>Jquery专区</li>
            </ul>
        </p>
    </p>
</body>
</html>

语法二:

代码如下:

$(selector).parentsUntil(element,filter)

参数列表:

##Parameter Description
expr Optional. 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>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("li").parentsUntil(document.getElementById("inner")).css("border","1px solid blue");
    })
</script>
</head>
<body>
    <p class="outer">
        <p>脚本之家欢迎您</p>
        <p id="inner">
            <ul class="first">
                <li>HTML专区</li>
                <li>Javascript专区</li>
                <li>p+CSS专区</li>
                <li>Jquery专区</li>
            </ul>
            <ul class="second">
                <li>HTML专区</li>
                <li>Javascript专区</li>
                <li>p+CSS专区</li>
                <li>Jquery专区</li>
            </ul>
        </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>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("li").parentsUntil(document.getElementById("inner"),".second").css("border","1px solid blue");
    })
</script>
</head>
<body>
    <p class="outer">
        <p>脚本之家欢迎您</p>
        <p id="inner">
            <ul class="first">
                <li>HTML专区</li>
                <li>Javascript专区</li>
                <li>p+CSS专区</li>
                <li>Jquery专区</li>
            </ul>
            <ul class="second">
                <li>HTML专区</li>
                <li>Javascript专区</li>
                <li>p+CSS专区</li>
                <li>Jquery专区</li>
            </ul>
        </p>
    </p>
</body>
</html>


The above is the detailed content of Tutorial on using parentsUntil() 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