jQuery traversa...LOGIN

jQuery traversal siblings()

jQuery is a collection object. If you want to quickly find the sibling elements of each element in the specified element collection, you can use the siblings() method

Understand the node search relationship:

As follows The blue class="item-2" li element, the red node is its sibling sibling node

<ul class="level-3">

<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3" >3</li>

</ul>

siblings() no parameters

Get a set of matching elements that contains each element Element collection of sibling elements

Note: jQuery is a collection object, so siblings is the sibling element that matches each element in the collection

siblings() method selectively accepts the same Type selector expression

Similarly because jQuery is a collection object, it may be necessary to filter the collection object to find the target element, so it is allowed to pass a selector expression

Let’s write an example below:

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        .left {
            width: auto;
            height: 150px;
        }
        
        .left div {
            width: 150px;
            height: 100px;
            padding: 5px;
            margin: 5px;
            float: left;
            background: #bbffaa;
            border: 1px solid #ccc;
        }
        
        a {
            display: block;
        }
    </style>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>

<body>
    <h2>siblings方法()</h2>
    <div class="left first-div">
        <div class="div">
            <ul class="level-1">
                <li class="item-1">1</li>
                <li class="item-2">2</li>
                <li class="item-3">3</li>
            </ul>
        </div>
        <div class="div">
            <ul class="level-2">
                <li class="item-1">1</li>
                <li class="item-2">2</li>
                <li class="item-3">3</li>
            </ul>
        </div>
        <div class="div">
            <ul class="level-3">
                <li class="item-1">1</li>
                <li class="item-2">2</li>
                <li class="item-3">3</li>
            </ul>
        </div>
    </div>
    <button>点击:siblingsv无参数</button>
    <button>点击:siblings传递选择器</button>
    <script type="text/javascript">
    $("button:first").click(function() {
        $('.item-2').siblings().css('border', '2px solid red');
    })
    </script>

    <script type="text/javascript">
    $("button:last").click(function() {
        //找到class=item-2的所有兄弟节点
        //然后筛选出最后一个,加上蓝色的边
       $('.item-2').siblings(':last').css('border', '2px solid blue');
    })
    </script>

</body>

</html>

Note: sibings finds all sibling elements, not adjacent sibling elements

Next Section
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title></title> <style> .left { width: auto; height: 150px; } .left div { width: 150px; height: 100px; padding: 5px; margin: 5px; float: left; background: #bbffaa; border: 1px solid #ccc; } a { display: block; } </style> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> </head> <body> <h2>siblings方法()</h2> <div class="left first-div"> <div class="div"> <ul class="level-1"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </div> <div class="div"> <ul class="level-2"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </div> <div class="div"> <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </div> </div> <button>点击:siblingsv无参数</button> <button>点击:siblings传递选择器</button> <script type="text/javascript"> $("button:first").click(function() { $('.item-2').siblings().css('border', '2px solid red'); }) </script> <script type="text/javascript"> $("button:last").click(function() { //找到class=item-2的所有兄弟节点 //然后筛选出最后一个,加上蓝色的边 $('.item-2').siblings(':last').css('border', '2px solid blue'); }) </script> </body> </html>
submitReset Code
ChapterCourseware