jQuery的強大選取器詳解

來源:互聯網
上載者:User

 

一 基本選取器

   $("input“) :選擇所有是input標籤的元素

   $("#input1"):選擇id為input1的元素

   $(".acss"):選擇所有包含acss 這個css類樣式的     

代碼

<body>                                                                  
<a href="">link</a>
<input id="input1" class="acss">
<input id="Text1" class="acss">
<input id="Text2" >    
<script>
var oo = $("input"); //oo是以上3個的集合
var pp = $("#input1");//pp是第一個
var qq = $(".acss");//qq 是前兩個的集合
</script>                                  
</body>

 

可以用以上3個進行組合式的查詢

var ww = $("input.acss");
//選擇具有acss的input標籤元素
var ee  = $("input#Text1.acss");//選擇具有acss 的 id為 Text1的 標籤為input的元素

 

二、子選取器

     父節點和直接子節點用(>)隔開,即實現子選取器方式

 

代碼

<p class="acss">
<a href="" id="a1"></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
</p>
<p>
<a href="" id="a2"></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
</p>
<script>
var oo = $("p a "); //選擇了p下面的所有的a
var pp = $("p>a"); //選擇了2個a元素 ,id為a1 和a2
var qq = $("p.acss a"); //選擇了id為a1的元素
</script>      

 

三、特徵選取器

   根據元素特徵進行選擇 a[href^=http://]

 

代碼


<div>       
<input id="Text3" name="myInput" />
<input id="Text5" name="myput" />
<input id="Text4" name="yourInput" />
<a id="a3" href="http://www.baidu.com"></a>
<a id="a4" href="www.baidu.com"></a>
</div>
<script>
var oo = $("a[href^=http://]");   //選擇href以 http:// 開頭的a。a3選中

var pp = $("input[name$=Input]"); //選擇name以 Input 結尾的input 。Text3,Text4選中

var qq = $("a[href*=com]");       //選擇href以 包含com的a 。a3,a4選中

var ww = $("input[id=Text3]");    //選擇正好等於的

</script>                 

 

另外 jQuery還提供了 has方法,如上面例子中

        div:has(input) 含義是。選擇包含input的所有div

       注意: div input 是選擇的是div中的所有input 元素

四、位置選取器

    位置選取器主要是根據元素的位置進行選擇,

         div a:first    返回頁面第一個在div中的a

        div a last    返回頁面最後一個在div中的a

         div odd      返回頁面偶數位置的div

        div even     返回頁面奇數位置的idv

        div first-child 返回div 中第一個子選擇

        div last-child 返回div 中最後一個子選擇

        only-child 沒有兄弟節點的元素

        nth-child(n):第n個子節點

        eq(n) 第n個匹配元素

        gt(n) n之後的匹配元素 不包含

         lt(n) n之前的匹配元素 不包含

 

五、jQuery自訂選擇

名稱 說明 解釋
:input 匹配所有 input, textarea, select 和 button 元素 尋找所有的input元素:
$(":input")
:text 匹配所有的文字框 尋找所有文字框:
$(":text")
:password 匹配所有密碼框 尋找所有密碼框:
$(":password")
:radio 匹配所有選項按鈕 尋找所有選項按鈕
:checkbox 匹配所有複選框 尋找所有複選框:
$(":checkbox")
:submit 匹配所有提交按鈕 尋找所有提交按鈕:
$(":submit")
:image

匹配所有映像域

匹配所有映像域:
$(":image")
:reset 匹配所有重設按鈕 尋找所有重設按鈕:
$(":reset")
:button 匹配所有按鈕 尋找所有按鈕:
$(":button")
:file 匹配所有檔案域 尋找所有檔案域:
$(":file")
名稱 說明 解釋
:enabled

匹配所有可用元素

尋找所有可用的input元素:
$("input:enabled")
:disabled 匹配所有不可用元素 尋找所有停用input元素:
$("input:disabled")
:checked 匹配所有選中的被選中元素(複選框、單選框等,不包括select中的option) 尋找所有選中的複選框元素:
$("input:checked")
:selected 匹配所有選中的option元素 尋找所有選中的選項元素:
$("select option:selected")

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.