jQuery選擇器的使用

使用jQuery選取元素時,須要使用jQuery選擇器。jquery

最經常使用的:動畫

一、thisthis

$(this),表明選取當前元素spa

二、id選擇器:ci

$("#myId"),表明選取id爲‘myId’的元素element

三、class選擇器:字符串

 $(".myClass"),表明選取全部class爲‘myClass’的元素get

四、根據type查找:input

$("input[type='checkbox']"),查找全部type爲checkbox的input元素,這與寫法:$(":checkbox")等價it

五、根據tagName查找:

$("p"),查找全部的p標籤

 

如下是經常使用jQuery選擇器的清單,詳情能夠參考w3c連接: https://www.w3school.com.cn/jquery/jquery_selectors.asp

jQuery 選擇器

選擇器 實例 選取
* $("*") 全部元素
#id $("#lastname") id="lastname" 的元素
.class $(".intro") 全部 class="intro" 的元素
element $("p") 全部 <p> 元素
.class.class $(".intro.demo") 全部 class="intro" 且 class="demo" 的元素
     
:first $("p:first") 第一個 <p> 元素
:last $("p:last") 最後一個 <p> 元素
:even $("tr:even") 全部偶數 <tr> 元素
:odd $("tr:odd") 全部奇數 <tr> 元素
     
:eq(index) $("ul li:eq(3)") 列表中的第四個元素(index 從 0 開始)
:gt(no) $("ul li:gt(3)") 列出 index 大於 3 的元素
:lt(no) $("ul li:lt(3)") 列出 index 小於 3 的元素
:not(selector) $("input:not(:empty)") 全部不爲空的 input 元素
     
:header $(":header") 全部標題元素 <h1> - <h6>
:animated   全部動畫元素
     
:contains(text) $(":contains('W3School')") 包含指定字符串的全部元素
:empty $(":empty") 無子(元素)節點的全部元素
:hidden $("p:hidden") 全部隱藏的 <p> 元素
:visible $("table:visible") 全部可見的表格
     
s1,s2,s3 $("th,td,.intro") 全部帶有匹配選擇的元素
     
[attribute] $("[href]") 全部帶有 href 屬性的元素
[attribute=value] $("[href='#']") 全部 href 屬性的值等於 "#" 的元素
[attribute!=value] $("[href!='#']") 全部 href 屬性的值不等於 "#" 的元素
[attribute$=value] $("[href$='.jpg']") 全部 href 屬性的值包含以 ".jpg" 結尾的元素
     
:input $(":input") 全部 <input> 元素
:text $(":text") 全部 type="text" 的 <input> 元素
:password $(":password") 全部 type="password" 的 <input> 元素
:radio $(":radio") 全部 type="radio" 的 <input> 元素
:checkbox $(":checkbox") 全部 type="checkbox" 的 <input> 元素
:submit $(":submit") 全部 type="submit" 的 <input> 元素
:reset $(":reset") 全部 type="reset" 的 <input> 元素
:button $(":button") 全部 type="button" 的 <input> 元素
:image $(":image") 全部 type="image" 的 <input> 元素
:file $(":file") 全部 type="file" 的 <input> 元素
     
:enabled $(":enabled") 全部激活的 input 元素
:disabled $(":disabled") 全部禁用的 input 元素
:selected $(":selected") 全部被選取的 input 元素
:checked $(":checked") 全部被選中的 input 元素
相關文章
相關標籤/搜索