jQuery 選擇器
jQuery Selectors
使用我們的 jQuery 選擇器測試器 來演示不同的選擇器。
選擇器 | 示例 | 選擇 |
---|---|---|
* | $("*") | 所有元素 |
#id | $("#lastname") | id="lastname" 的元素 |
.class | $(".intro") | 所有 class="intro" 的元素 |
.class,.class | $(".intro,.demo") | 所有 class 為 "intro" 或 "demo" 的元素 |
元素 | $("p") | 所有 <p> 元素 |
el1,el2,el3 | $("h1,div,p") | 所有 <h1>、<div> 和 <p> 元素 |
:first | $("p:first") | 第一個 <p> 元素 |
:last | $("p:last") | 最後一個 <p> 元素 |
:even | $("tr:even") | 所有偶數 <tr> 元素 |
:odd | $("tr:odd") | 所有奇數 <tr> 元素 |
:first-child | $("p:first-child") | 所有是其父元素第一個子元素的 <p> 元素 |
:first-of-type | $("p:first-of-type") | 所有是其父元素第一個 <p> 元素的 <p> 元素 |
:last-child | $("p:last-child") | 所有是其父元素最後一個子元素的 <p> 元素 |
:last-of-type | $("p:last-of-type") | 所有是其父元素最後一個 <p> 元素的 <p> 元素 |
:nth-child(n) | $("p:nth-child(2)") | 所有是其父元素第二個子元素的 <p> 元素 |
:nth-last-child(n) | $("p:nth-last-child(2)") | 所有是其父元素第二個子元素的 <p> 元素(從最後一個子元素開始計數) |
:nth-of-type(n) | $("p:nth-of-type(2)") | 所有是其父元素第二個 <p> 元素的 <p> 元素 |
:nth-last-of-type(n) | $("p:nth-last-of-type(2)") | 所有是其父元素第二個 <p> 元素的 <p> 元素(從最後一個子元素開始計數) |
:only-child | $("p:only-child") | 所有是其父元素唯一子元素的 <p> 元素 |
:only-of-type | $("p:only-of-type") | 所有是其父元素唯一子型別元素的 <p> 元素 |
parent > child | $("div > p") | 所有是 <div> 元素直接子元素的 <p> 元素 |
parent descendant | $("div p") | 所有是 <div> 元素後代的 <p> 元素 |
element + next | $("div + p") | 緊跟在每個 <div> 元素後面的 <p> 元素 |
element ~ siblings | $("div ~ p") | 出現在 <div> 元素之後的所有 <p> 元素 |
:eq(index) | $("ul li:eq(3)") | 列表中的第四個元素(索引從 0 開始) |
:gt(no) | $("ul li:gt(3)") | 索引大於 3 的列表元素 |
:lt(no) | $("ul li:lt(3)") | 索引小於 3 的列表元素 |
:not(selector) | $("input:not(:empty)") | 所有非空輸入元素 |
:header | $(":header") | 所有標題元素 <h1>, <h2> ... |
:animated | $(":animated") | 所有動畫元素 |
:focus | $(":focus") | 當前獲得焦點的元素 |
:contains(text) | $(":contains('Hello')") | 包含文字 "Hello" 的所有元素 |
:has(selector) | $("div:has(p)") | 所有包含 <p> 元素的 <div> 元素 |
:empty | $(":empty") | 所有空元素 |
:parent | $(":parent") | 所有是另一個元素父元素的元素 |
:hidden | $("p:hidden") | 所有隱藏的 <p> 元素 |
:visible | $("table:visible") | 所有可見的表格 |
:root | $(":root") | 文件的根元素 |
:lang(language) | $("p:lang(de)") | 所有 lang 屬性值以 "de" 開頭的 <p> 元素 |
[attribute] | $("[href]") | 所有具有 href 屬性的元素 |
[attribute=value] | $("[href='default.htm']") | 所有 href 屬性值等於 "default.htm" 的元素 |
[attribute!=value] | $("[href!='default.htm']") | 所有 href 屬性值不等於 "default.htm" 的元素 |
[attribute$=value] | $("[href$='.jpg']") | 所有 href 屬性值以 ".jpg" 結尾的元素 |
[attribute|=value] | $("[title|='Tomorrow']") | 所有 title 屬性值等於 "Tomorrow" 或以 "Tomorrow" 開頭後跟連字元的元素 |
[attribute^=value] | $("[title^='Tom']") | 所有 title 屬性值以 "Tom" 開頭的元素 |
[attribute~=value] | $("[title~='hello']") | 所有 title 屬性值包含特定單詞 "hello" 的元素 |
[attribute*=value] | $("[title*='hello']") | 所有 title 屬性值包含單詞 "hello" 的元素 |
:input | $(":input") | 所有輸入元素 |
:text | $(":text") | 所有 type="text" 的輸入元素 |
:password | $(":password") | 所有 type="password" 的輸入元素 |
:radio | $(":radio") | 所有 type="radio" 的輸入元素 |
:checkbox | $(":checkbox") | 所有 type="checkbox" 的輸入元素 |
:submit | $(":submit") | 所有 type="submit" 的輸入元素 |
:reset | $(":reset") | 所有 type="reset" 的輸入元素 |
:button | $(":button") | 所有 type="button" 的輸入元素 |
:image | $(":image") | 所有 type="image" 的輸入元素 |
:file | $(":file") | 所有 type="file" 的輸入元素 |
:enabled | $(":enabled") | 所有啟用的輸入元素 |
:disabled | $(":disabled") | 所有停用的輸入元素 |
:selected | $(":selected") | 所有選中的輸入元素 |
:checked | $(":checked") | 所有已勾選的輸入元素 |