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") | 所有已勾选的输入元素 |