jQuery hierarch...LOGIN

jQuery hierarchical selector

Level selector


##$(s1 s2) [Father and Son]

Derived selector: Get all s2 nodes inside s1 (regardless of level)

$("div span")


<div>
    <span></span>
    <p>
        <span></span>
    </p>
</div>
<span></span>


$(s1 > s2) [parent and child]
##Direct child element selector: in s1 Internally obtain the child element node of s2

$("div > span")

    <div>
        <span></span>
        <p>
            <span></span>
        </p>
        <span></span>
    </div>
<span></span>


$(s1 + s2) [Brother]
Direct sibling selector: Get the s2 node of the first sibling relationship immediately after s1. There cannot be any other tags between s1 and s2, otherwise Not working

$("div + span")

<div>
    <span></span>
    <p>
        <span></span>
    </p>
    <span></span>
</div>
<span></span>
<span></span>
<div></div>
<span></span>


##$(s1 ~ s2 ) [Brothers]

Subsequent all sibling relationship node selectors: Get all s2 nodes of sibling relationships behind s1

$("div ~ span" )

<div>
    <span></span>
    <p>
        <span></span>
    </p>
    <span></span>
</div>
<span></span>
<span></span>
<p></p>
<span></span>
<!DOCTYPE html>
<html>
    <head>
        <title>php.cn</title>
        <meta charset="utf-8" />
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
        <script >
        function f1(){
            //$(s1 s2)
            $('div span').css('background-color','blue');

            //$(s1 > s2) 
            $('div > span').css('background-color','green');//张飞 关羽

            //$(s1 + s2) 紧紧挨着的
            $('div + span').css('background-color','red');

            //$(s1 ~ s2)
           $('div ~ span').css('background-color','brown');
        }
        </script>
    </head>
    <body>
        <h2>层次选择器</h2>
        <div>
            <span>北京</span>
            <p>
                <span>上海</span>
            </p>
            <span>深圳</span>
        </div>
        <p>南京</p>
        <span>杭州</span>
        <p>厦门</p>
        <span>天津</span>
        <div>重庆</div>
        <span>合肥</span>
        <input type="button" value="触发" onclick="f1()" />
    </body>
</html>

Note: Please comment out the other three of the four selectors. Test one by one

Next Section

<!DOCTYPE html> <html> <head> <title>php.cn</title> <meta charset="utf-8" /> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script > function f1(){ //$(s1 s2) $('div span').css('background-color','blue'); //$(s1 > s2) $('div > span').css('background-color','green');//张飞 关羽 //$(s1 + s2) 紧紧挨着的 $('div + span').css('background-color','red'); //$(s1 ~ s2) $('div ~ span').css('background-color','brown'); } </script> </head> <body> <h2>层次选择器</h2> <div> <span>北京</span> <p> <span>上海</span> </p> <span>深圳</span> </div> <p>南京</p> <span>杭州</span> <p>厦门</p> <span>天津</span> <div>重庆</div> <span>合肥</span> <input type="button" value="触发" onclick="f1()" /> </body> </html>
submitReset Code
ChapterCourseware