首页 > 应用开发 > Javascript > jQuery 选择器
请使用我们的 jQuery 选择器检测器
jQuery 选择器检测器 来演示不同的选择器。
| 选择器 | 实例 | 选取 |
|
|
|---|---|---|---|---|
| * * | $("*") | 所有元素 |
|
|
| # id #id | $("#lastname") | id="lastname" 的元素 |
|
|
| . class .class | $(".intro") | class="intro" 的所有元素 |
|
|
| . class,. class .class,.class | $(".intro,.demo") | class 为 "intro" 或 "demo" 的所有元素 |
|
|
| element element | $("p") | 所有 <p> 元素 |
|
|
| el1, el2, el3 el1,el2,el3 | $("h1,div,p") | 所有 <h1>、<div> 和 <p> 元素 |
|
|
|
|
|
|
|
|
| :first :first | $("p:first") | 第一个 <p> 元素 |
|
|
| :last :last | $("p:last") | 最后一个 <p> 元素 |
|
|
| :even :even | $("tr:even") | 所有偶数 <tr> 元素 |
|
|
| :odd :odd | $("tr:odd") | 所有奇数 <tr> 元素 |
|
|
|
|
|
|
|
|
| :first-child :first-child | $("p:first-child") | 属于其父元素的第一个子元素的所有 <p> 元素 |
|
|
| :first-of-type :first-of-type | $("p:first-of-type") | 属于其父元素的第一个 <p> 元素的所有 <p> 元素 |
|
|
| :last-child :last-child | $("p:last-child") | 属于其父元素的最后一个子元素的所有 <p> 元素 |
|
|
| :last-of-type :last-of-type | $("p:last-of-type") | 属于其父元素的最后一个 <p> 元素的所有 <p> 元素 |
|
|
| :nth-child( n) :nth-child(n) | $("p:nth-child(2)") | 属于其父元素的第二个子元素的所有 <p> 元素 |
|
|
| :nth-last-child( n) :nth-last-child(n) | $("p:nth-last-child(2)") | 属于其父元素的第二个子元素的所有 <p> 元素,从最后一个子元素开始计数 |
|
|
| :nth-of-type( n) :nth-of-type(n) | $("p:nth-of-type(2)") | 属于其父元素的第二个 <p> 元素的所有 <p> 元素 |
|
|
| :nth-last-of-type( n) :nth-last-ot-type | $("p:nth-last-of-type(2)") | 属于其父元素的第二个 <p> 元素的所有 <p> 元素,从最后一个子元素开始计数 |
|
|
| :only-child :only-child | $("p:only-child") | 属于其父元素的唯一子元素的所有 <p> 元素 |
|
|
| :only-of-type :only-of-type | $("p:only-of-type") | 属于其父元素的特定类型的唯一子元素的所有 <p> 元素 |
|
|
|
|
|
|
|
|
| parent > child :parent>child | $("div > p") | <div> 元素的直接子元素的所有 <p> 元素 |
|
|
| parent descendant parent descendan | $("div p") | <div> 元素的后代的所有 <p> 元素 |
|
|
| element + next element + next | $("div + p") | 每个 <div> 元素相邻的下一个 <p> 元素 |
|
|
| element ~ siblings element~siblings | $("div ~ p") | <div> 元素同级的所有 <p> 元素 |
|
|
|
|
|
|
|
|
| :eq( index) :eq(index) | $("ul li:eq(3)") | 列表中的第四个元素(index 值从 0 开始) |
|
|
| :gt( no) :gt(no) | $("ul li:gt(3)") | 列举 index 大于 3 的元素 |
|
|
| :lt( no) :lt(no) | $("ul li:lt(3)") | 列举 index 小于 3 的元素 |
|
|
| :not( selector) :not(selector) | $("input:not(:empty)") | 所有不为空的输入元素 |
|
|
|
|
|
|
|
|
| :header :header | $(":header") | 所有标题元素 <h1>, <h2> ... |
|
|
| :animated :animated | $(":animated") | 所有动画元素 |
|
|
| :focus :focus | $(":focus") | 当前具有焦点的元素 |
|
|
| :contains( text) :contains(text) | $(":contains('Hello')") | 所有包含文本 "Hello" 的元素 |
|
|
| :has( selector) :has(selector) | $("div:has(p)") | 所有包含有 <p> 元素在其内的 <div> 元素 |
|
|
| :empty :empty | $(":empty") | 所有空元素 |
|
|
| :parent :parent | $(":parent") | 选择所有含有子元素或者文本的父级元素。
|
|
|
| :hidden :hidden | $("p:hidden") | 所有隐藏的 <p> 元素 |
|
|
| :visible :visible | $("table:visible") | 所有可见的表格 |
|
|
| :root :root | $(":root") | 文档的根元素 |
|
|
| :lang( language) :lang(language) | $("p:lang(de)") | 所有带有以 "de" 开头的 lang 属性值的 <p> 元素 |
|
|
|
|
|
|
|
|
| [ attribute] [attribute] | $("[href]") | 所有带有 href 属性的元素 |
|
|
| [ attribute= value] [attribute=value] | $("[href='default.htm']") | 所有带有 href 属性且值等于 "default.htm" 的元素 |
|
|
| [ attribute!= value] [attribute!=value] | $("[href!='default.htm']") | 所有带有 href 属性且值不等于 "default.htm" 的元素 |
|
|
| [ attribute$= value] [attribute$=value] | $("[href$='.jpg']") | 所有带有 href 属性且值以 ".jpg" 结尾的元素 |
|
|
|
[attribute|=value] |
$("[title|='Tomorrow']") | 所有带有 title 属性且值等于 'Tomorrow' 或者以 'Tomorrow' 后跟连接符作为开头的字符串 |
|
|
| [ attribute^= value] [attribute^=value] | $("[title^='Tom']") | 所有带有 title 属性且值以 "Tom" 开头的元素 |
|
|
| [ attribute~= value] [attribute~=value] | $("[title~='hello']") | 所有带有 title 属性且值包含单词 "hello" 的元素 |
|
|
| [ attribute*= value] [attribute*=value] | $("[title*='hello']") | 所有带有 title 属性且值包含字符串 "hello" 的元素 |
|
|
|
|
|
|
|
|
| :input :input | $(":input") | 所有 input 元素 |
|
|
| :text :text | $(":text") | 所有带有 type="text" 的 input 元素 |
|
|
| :password :password | $(":password") | 所有带有 type="password" 的 input 元素 |
|
|
| :checkbox :checkbox | $(":checkbox") | 所有带有 type="checkbox" 的 input 元素 |
|
|
| :submit :submit | $(":submit") | 所有带有 type="submit" 的 input 元素 |
|
|
| :reset :reset | $(":reset") | 所有带有 type="reset" 的 input 元素 |
|
|
| :button :button | $(":button") | 所有带有 type="button" 的 input 元素 |
|
|
| :image :image | $(":image") | 所有带有 type="image" 的 input 元素 |
|
|
| :file :file | $(":file") | 所有带有 type="file" 的 input 元素 |
|
|
| :enabled :enabled | $(":enabled") | 所有启用的 input 元素 |
|
|
| :disabled :disabled | $(":disabled") | 所有禁用的 input 元素 |
|
|
| :selected :selected | $(":selected") | 所有选定的 input 元素 |
|
|
| :checked :checked | $(":checked") | 所有选中的 input 元素 |
|
|
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70018469/viewspace-2909145/ ,如需转载,请注明出处,否则将追究法律责任。