jQuery 选择器

1.ID 选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("#div1").css("background", "red")
            })
        </script>
    </head>
    <body>
        <div id="div1">测试盒子</div>
    </body>
</html>

9BC3C215-4BEF-44B1-A6C3-ACE331209598

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("#a\\.b").css("color", "red")
                $("#a\\:b").css("color", "red")
                $("#\\[div\\]").css("color", "red")
            })
        </script>
    </head>
    <body>
        <div id="a.b">div 1</div>
        <div id="a:b">div 2</div>
        <div id="[div]">div 3</div>
    </body>
</html>

B910CE26-C2FF-472D-A264-E8A57BECDC4F

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                document.getElementById("a.b").style.color = "red"
                document.getElementById("a:b").style.color = "red"
                document.getElementById("[div]").style.color = "red"
            })
        </script>
    </head>
    <body>
        <div id="a.b">div 1</div>
        <div id="a:b">div 2</div>
        <div id="[div]">div 3</div>
    </body>
</html>

原生的JavaScript 就无需转换符号~

2.标签选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("div").css("color","red")
            })
        </script>
    </head>
    <body>
        <div>div 1</div>
        <div>div 2</div>
        <div>div 3</div>
    </body>
</html>

3.类选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $(".red").css("color","red");
            })
        </script>
    </head>
    <body>
        <div class="red">div 1</div>
        <div>div 2</div>
        <div class="red">div 3</div>
    </body>
</html>

93FFFDD2-E8D1-4D59-9AFB-30AE45BA7457

4.通配选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("body *").css("color", "red")
            })
        </script>
    </head>
    <body>
        <div>DIV</div>
        <span>SPAN</span>
        <p>P</p>
    </body>
</html>

7955AEC5-869A-4FAD-A1B5-DA287CCAF9C0

$("body *").css("color", "red")

可以等效于

var all = document.getElementsByTagName("*")
for (var i = 0; i < all.length; i++) {
    all[i].style.color = "red"
}

可以等效于

var all = document.getElementsByTagName("*")
$(all).css("color", "red")

5.组选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("h2,#wrap,span.red,[title='text'").css("color","red")
            })
        </script>
    </head>
    <body>
        <h2>H2</h2>
        <div id="wrap">DIV</div>
        <span class="red">SPAN</span>
        <p title="text">P</p>
    </body>
</html>

9ABB4941-39B8-45CA-9A7A-5CAF5DDFADD5

6.包含选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("form input").css({"border":"solid 1px red", "background": "blue"})
            })
        </script>
    </head>
    <body>
        <form>
            <fieldset>
                <label>
                    包含的子文本框
                    <input/>
                </label>
                <fieldset>
                    <label>
                        包含的孙文本框
                        <input/>
                    </label>
                </fieldset>
            </fieldset>
        </form>
        <label>
            非包含的文本框
            <input/>
        </label>
    </body>
</html>

AE91D2A1-D6D0-40B6-A2FA-4D0CAB1D577E

7.子选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("div > img").css("border", "solid 5px red")
            })
        </script>
    </head>
    <body>
        <div>
            <span><img src="../../img/c.jpg"/></span>
            <img src="../../img/c.jpg"/>
        </div>
        <img src="../../img/c.jpg"/>
    </body>
</html>

406E948E-D341-4E28-AADA-E7DE692E9087

8.相邻选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("div + img").css("border", "solid 5px red")
            })
        </script>
    </head>
    <body>
        <div>
            <span><img src="../../img/c.jpg"/></span>
            <img src="../../img/c.jpg"/>
        </div>
        <img src="../../img/c.jpg"/>
        <img src="../../img/c.jpg"/>
    </body>
</html>

A9639574-CBBB-4C32-B670-11BBC1C1485A

9.兄弟选择器

<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="../../Library/jquery-3.3.1.min.js"></script>
        <script>
            $(function(){
                $("div ~ img").css("border", "solid 5px red")
            })
        </script>
    </head>
    <body>
        <div>
            <span><img src="../../img/c.jpg"/></span>
            <img src="../../img/c.jpg"/>
        </div>
        <img src="../../img/c.jpg"/>
        <img src="../../img/c.jpg"/>
    </body>
</html>

2EC83979-5371-4FE5-81EB-9F52E43F0B25