天天看點

jQuery 常用選擇器的用法

在使用jquery的時候一定要進行正确的安裝

dom對象與jquery的互相轉換

選擇器

标記選擇器:css中的元素(p,h,span…..)

id選擇器:給元素一個id名(#id名稱)

類選擇器: 給元素一個class名(.class名)

*$(““):選取所有的**

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <script>
            $(function(){
                $("button").click(function(){//給按鈕一個點選事件
                    $("*").hide();//隐藏
                });
            });
        </script>
    </head>
    <body>
        <h3>我是h3标簽</h3>
        <p>我是p标簽</p>
        <button>點我</button>
    </body>
</html>
           

$(“h3:first”) :選取第一個h3元素

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <script>
            $(function(){
                $("button").click(function(){
                    $("h3:first").hide();
                });
            });
        </script>

    </head>
    <body>
        <h3>我是h3标簽</h3>
        <h3>我也是h3标簽</h3>
        <p>我是p标簽</p>
        <button>點我</button>
    </body>
</html>
           

相似的還有

$(“ul li:first”):無序清單下的第一個li

$(“[href]”):選取帶有href的(【】裡是什麼内容就選取什麼)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <script>
            $(function(){
                $("button").click(function(){
                    $("[href]").hide();
                });
            });
        </script>
    </head>
    <body>
        <a href="#">♪(^∇^*)</a>
        <button>點我</button>
    </body>
</html>
           
$(xx[屬性名='屬性值'])  比對屬性值
  $(xx[屬性名!='屬性值'])  不比對屬性值
  $(xx[屬性名^='字元串'])   屬性值以指定字元串開頭
  $(xx[屬性名$='字元串'])   屬性值以指定字元串結尾
  $(xx[屬性名*='字元串'])   屬性值包含指定字元串
           
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <script>
            $(function(){
                $("button").click(function(){
                    $("a[href='#001']").css("color","red");
//                  $("a[href!='#001']").css("color","yellow");
                    $("p[class^='o']").css("color","red");//以指定字元串開始
                    $("p[class$='o']").css("color","red");//以指定字元串結尾
                    $("p[class*='www']").css("color","red");//包含指定元素

                });
            });
        </script>
    </head>
    <body>
        <a href="#001">O(∩_∩)O哈哈~</a><br />
        <a href="#002">O(∩_∩)O哈哈~</a><br />
        <a href="#003">O(∩_∩)O哈哈~</a><br />
        <p class="one">我是class名為one的p标記</p>
        <p class="two">我是class名為two的p标記</p>
        <p class="www">我是用來測試包含指定字元</p>
        <button>點我</button>
    </body>
</html>

           

** (“xxx:eq(index)”)選擇等于目前索引元素(從0開始) ( “ x x x : e q ( i n d e x ) ” ) 選 擇 等 于 當 前 索 引 元 素 ( 從 0 開 始 ) (“xxx:gt(index)”) 選擇大于目前索引元素(從0開始)

(“xxx:lt(index)”)選擇小于目前索引元素(從0開始) ( “ x x x : l t ( i n d e x ) ” ) 選 擇 小 于 當 前 索 引 元 素 ( 從 0 開 始 ) (“xxx:header”) 選擇所有标題元素(h1~h6)**

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <script>
            $(function(){
                $("button").click(function(){
                     $("li:eq(0)").css("color","blue");//指定索引
                     $("li:gt(2)").css("color","blue");//大于指定索引
                     $("li:lt(3)").css("color","blue");//小于指定索引
                });
            });
        </script>

    </head>
    <body>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <button>點選</button>
    </body>
</html>