jQuery 2 选择器
选择器 | 实例 | 选取 |
基础选择器 | ||
* | $("*") | 所有元素 |
#id | $("#lastname") | id="lastname" 的元素 |
.class | $(".intro") | 所有 class="intro"的元素 |
element | $("p") | 所有 <p> 元素 |
s1,s2,s3 | $("th,td,.intro") | 所有<th>或<td>或class="intro"的元素 |
s1s2s3
中间没空格 |
$("p.intro") $("#id.intro") $(":input.intro") $("form :input.intro") |
所有<p>且class="intro"的元素 所有id="di"且class="intro"的元素 所有<input>且class="intro"的元素 所有<form>里的所有<input>且class="intro"的元素 |
层次选择器 | ||
ancestor descentdant | $("div span") | <div>里的所有<span>,后代选择器 |
parent > child | $("div > span") | <div>的子元素里的所有<span>,子元素选择器 |
pre + next | $("#one + div") | id="one"的元素的下一个紧挨着的兄弟元素<div> |
pre ~ siblings | $("#one ~ div") | id="one"的元素后面的所有兄弟元素<div> |
基本过滤选择器 | ||
:first | $("p:first") | 第一个 <p> 元素,只找第一个匹配到的 |
:last | $("p:last") | 最后一个 <p> 元素,只找第一个匹配到的 |
:even | $("tr:even") | 所有偶数 <tr> 元素(index从0开始) |
:odd | $("tr:odd") | 所有奇数 <tr> 元素(index从0开始) |
:eq(index) | $("ul li:eq(3)") | 列表中的第四个元素(index 从 0 开始),只找第一个匹配到的
()里必须是数字,不能是变量,如:var a=2; $("ul li:eq(a)")错误 |
:gt(no) | $("ul li:gt(3)") | 列出 index 大于 3 的元素(index从0开始) ()里必须是数字,不能是变量 |
:lt(no) | $("ul li:lt(3)") | 列出 index 小于 3 的元素(index从0开始) ()里必须是数字,不能是变量 |
:not(selector) | $("input:not(:empty)") | 所有不为空的 input 元素 |
:header | $(":header") | 所有标题元素 <h1> - <h6> |
:animated |
| 所有动画元素 |
内容过滤选择器 | ||
:contains(text) | $("div:contains('我')") | 包含文本‘我’的所有<div>元素 |
:empty | $("div:empty") | 无子(元素)节点的所有<div>元素 |
:has(selector) | $("div:has(p)") | 含有<p>元素的所有<div>元素 |
:parent | $("div:parent") | 含有子元素(包括文本元素)的<div>元素 |
可见性过滤选择器 | ||
:hidden | $("p:hidden") $(":hidden") | 所有隐藏的 <p> 元素 所有不可见元素 |
:visible | $("table:visible") | 所有可见的<table>元素 |
属性过滤选择器 | ||
[attribute] | $("div[class]") | 所有带有 class 属性的<div>元素 |
[attribute=value] | $("[href='#']") | 所有 href 属性的值等于 "#" 的元素 |
[attribute!=value] | $("a[href!='#']") | 所有 href 属性的值不等于 "#"的<a>元素 |
[attribute$=value] | $("[href$='.jpg']") | 所有 href 属性的值以 ".jpg"结尾的元素 |
[attribute^=value] | $("div[title^='aaa']") | 所有 title 属性的值以 "aaa"开头的<div>元素 |
[attribute*=value] | $("div[title^='aaa']") | 所有 title 属性的值中有 "aaa"的<div>元素 |
[selector1][selector2]··· | $("div[class][href='#']") | 所有带有 class 属性且 href 属性的值等于 "#" 的<div>元素 |
子元素过滤选择器 | ||
:nth-child(index) | $("ul li:nth-child(1)") | 所有的<ul>下第1个子元素<li>,index从1开始 ()里必须是数字,不能是变量 |
:nth-child(even) | $("ul li:nth-child(even)") | 所有<ul>下的所有偶数<li>,index从1开始 |
:nth-child(odd) | $("ul li:nth-child(odd)") | 所有<ul>下的所有奇数<li>,index从1开始 |
:nth-child(equation) | $("ul li:nth-child(3n+1)") | 所有<ul>下的所有索引是3n+1的<li>,index从1开始,n从0开始 |
:first-child | $("ul li:first-child") | 所有的<ul>下第1个子元素<li> |
:last-child | $("ul li:last-child") | 所有的<ul>下最后1个子元素<li> |
:only-child | $("ul li:only-child") | 所有的<ul>下唯一一个<li>元素 |
基本过滤选择器 :first :last :even :odd :eq(index)与上面的子元素过滤选择器区别: 基本过滤选择器只匹配第一个,子元素过滤选择器匹配所有。 对于下面的html代码: <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> <li>Tane</li> <li>Ralph</li> </ul> $("ul li:first").text();得到的结果为John. $("ul li:first-child").text();得到的结果为JohnGlen. | ||
表单对象属性选择器 | ||
:enabled | $("input:enabled") | 所有可用的 input 元素 |
:disabled | $("input':disabled") | 所有禁用的 input 元素 |
:checked | $("input:checked") | 所有被选中的 input 元素 |
:selected | $("option:selected") | 所有被选取的 option元素 |
表单选择器 | ||
: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> 元素 |
补充1:多参数的jQuery选择器
以上我们所说的选择器都是一个参数(一个””),jQuery允许多个参数。例如:
$(".loli", ".keakon", "div").css("color","red"); //等同于
$("div").find(".keakon").find(".loli").css("color","red"); //也等同于
$("div .keakon .loli").css("color","red"); //后代选择器
当然,最后的写法最简洁,也是用得最多的。然而最后的写法不能利用已有的jQuery对象和函数返回值,例如$(this)和$("input").parents("div:first"),所以当需要用到这些对象时,第一种写法便可以派上用场了(毕竟比find更简短)。
例如:
HTML代码:
<div id=”#aaa”><span></span></div>
JS代码:
$('#aaa').click(function(){
//如果想控制里面的span怎么办呢?
$('>span',this).addClass('bbb');
//这个就是取得当前元素里的某个符合条件的元素
});
补充2: index() 方法
格式1:
$(selector).index() //获得第一个匹配元素相对于其同胞元素的 index 位置。
例如:
$("li:eq(1)").index(); //$("li:eq(1)")相对于其同胞元素的 index 位置
格式2:
$(selector).index(element) //获得元素相对于选择器的 index 位置。
$("li").index($(this)); //$(this)相对于选择器$("li")的位置