点击"名称"会跳转到此方法的jQuery官方说明文档.
### 1. 基础选择器 Basics
<table cellspacing="1" cellpadding="5" width="767" border="1" style="margin:0px 0px 0px 10px; padding:0px; width:650px; border:1px solid silver; border-collapse:collapse; word-break:break-word; color:rgb(51,51,51); font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px"><tbody style="margin:0px; padding:0px"><tr style="margin:0px; padding:0px"><td valign="top" width="130" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">名称</span></td><td valign="top" width="365" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">说明</span></td><td valign="top" width="266" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">举例</span></td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="139" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/id#id" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">#id</a></span></td><td valign="top" width="360" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">根据元素Id选择</td><td valign="top" width="264" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$("divId") 选择ID为divId的元素</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="146" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/element#element" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">element</a></span></td><td valign="top" width="356" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">根据元素的名称选择,</td><td valign="top" width="262" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$("a") 选择所有<a>元素</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="152" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/class#class" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">.class</a></span></td><td valign="top" width="353" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">根据元素的css类选择</td><td valign="top" width="260" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$(".bgRed") 选择所用CSS类为bgRed的元素</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="157" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/all" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">*</a></span></td><td valign="top" width="351" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">选择所有元素</td><td valign="top" width="259" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$("*")选择页面所有元素</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="160" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/multiple#selector1selector2selectorN" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">selector1, <br style="margin:0px; padding:0px"/>selector2, <br style="margin:0px; padding:0px"/>selectorN</a></span></td><td valign="top" width="349" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">可以将几个选择器用","分隔开然后再拼成一个选择器字符串.会同时选中这几个选择器匹配的内容.</td><td valign="top" width="263" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$("#divId, a, .bgRed")</td></tr></tbody></table>
~~~
function f1(){
//通过【id选择器】获得节点
//$()符号就是jquery的函数,函数执行完毕返回对象
$("#bread").css('color','red');
//【class类别选择器】
$(".milk").css('color','blue');
//【tag标签选择器】
$("h2").css('color','green');
$("p").css('color','gold');
//【通配符选择器*】 ,获得全部节点
$("*").css("color",'blue');
//【联合选择器】
$("#bread,.milk,p").css('background-color','lightblue');
}
</script>
<style type="text/css">
#bread{}
.milk{}
p{}
*{}
#bread,.milk,p {}
</style>
</head>
<body>
<h2>基本选择器</h2>
<div id="bread">bread</div>
<div class="milk">milk</div>
<div>apple</div>
<p>banana</p>
<input type="button" value="触发" onclick="f1()">
</body>
~~~
[学习建议]: 大家暂时记住基础选择器即可, 可以直接跳到下一节"jQuery选择器实验室"进行动手练习, 以后再回来慢慢学习全部的选择器, 或者用到的时候再回来查询.
### 2.层次选择器 Hierarchy
<table cellspacing="1" cellpadding="5" width="774" border="1" style="margin:0px 0px 0px 10px; padding:0px; width:650px; border:1px solid silver; border-collapse:collapse; word-break:break-word; color:rgb(51,51,51); font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px"><tbody style="margin:0px; padding:0px"><tr style="margin:0px; padding:0px"><td valign="top" width="173" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">名称</span></td><td valign="top" width="321" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">说明</span></td><td valign="top" width="274" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">举例</span></td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="178" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/descendant#ancestordescendant" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">ancestor descendant</a></span></td><td valign="top" width="318" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">使用"form input"的形式选中form中的所有input元素.即ancestor(祖先)为from, descendant(子孙)为input.</td><td valign="top" width="273" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$(".bgRed div") 选择CSS类为bgRed的元素中的所有<div>元素.</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="181" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/child#parentchild" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">parent > child</a></span></td><td valign="top" width="316" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">选择parent的直接子节点child. child必须包含在parent中并且父类是parent元素.</td><td valign="top" width="272" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$(".myList>li") 选择CSS类为myList元素中的直接子节点<li>对象.</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="184" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/next#prevnext" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">prev + next</a></span></td><td valign="top" width="315" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">prev和next是两个同级别的元素. 选中在prev元素后面的next元素.</td><td valign="top" width="272" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$("#hibiscus+img")选在id为hibiscus元素后面的img对象.</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="185" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/siblings#prevsiblings" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">prev ~ siblings</a></span></td><td valign="top" width="314" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)"> </span>选择prev后面的根据siblings过滤的元素<br style="margin:0px; padding:0px"/>注:siblings是过滤器</td><td valign="top" width="274" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">$("#someDiv~[title]")选择id为someDiv的对象后面所有带有title属性的元素</td></tr></tbody></table>
实例:
**2.1 ancestor descendant:父子级关系**
派生选择器
~~~
$(“#bread .milk”)
<div id=bread><input class=milk><spanclass=milk>
<div><p class=milk></div>
</div>
~~~
在指定的父级选择器下边获得全部的派生子级(后代)元素
2.2 parent > child :父子级关系
在给定的父元素下匹配所有子元素.
~~~
$(“#bread >.milk”)
<div id=bread>
<input class=milk>
<spanclass=milk></span>
<div>
<p class=milk></p>
~~~
**2.3 prev + next:兄弟级关系**
匹配紧接在 所有prev 元素后的 next(下一个) 元素
~~~
$(“#bread + .milk”)
<div id=bread>
<span class=milk></span>
</div>
<p class=milk></p>
<spanclass=milk></span>
~~~
**2.4 prev ~ siblings:兄弟级关系**
匹配 prev 元素之后的所有 siblings 元素.
~~~
$(“#bread ~ .milk”)
<span class=milk>
<div id=bread>
<span class=milk></span>
</div>
<p class=milk></p>
<span class=milk></span>
<inputclass=milk />
~~~
~~~
function f1(){
//派生选择器$("sel1 sel2")
$("#bread .milk").css("color","red");
//子元素选择器$("sel1 > sel2")
$("#bread > .milk").css('color','red');
//匹配紧贴在一起的下一个元素
$("#bread + .milk").css("color",'red');
//匹配prev之后所有的兄弟节点
//$("prev ~ sibling")
$("#bread ~ .milk").css("color","red");
}
</script>
<style type="text/css">
</style>
</head>
<body>
<h2>层次选择器</h2>
<div id="bread">
<input type="text" value="hello" class="milk">
<span class="milk">world</span>
<div>
<span class="milk">pear</span>
</div>
</div>
<p class="milk">this is p tag</p>
<span class="milk">desk</span>
<div id="apple"><span class="milk">第二个span</span></div>
<input type="button" value="触发" onclick="f1()" />
</body>
~~~
### 3.基本过滤器 Basic Filters
<table cellspacing="1" cellpadding="5" width="776" border="1" style="margin:0px 0px 0px 10px; padding:0px; width:650px; border:1px solid silver; border-collapse:collapse; word-break:break-word; color:rgb(51,51,51); font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px"><tbody style="margin:0px; padding:0px"><tr style="margin:0px; padding:0px"><td valign="top" width="171" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">名称</span></td><td valign="top" width="207" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">说明</span></td><td valign="top" width="392" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">举例</span></td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="175" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/first" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:first</a></span></td><td valign="top" width="206" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">匹配找到的第一个元素</td><td valign="top" width="390" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找表格的第一行:$("tr:first")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="178" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/last" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:last</a></span></td><td valign="top" width="206" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">匹配找到的最后一个元素</td><td valign="top" width="389" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找表格的最后一行:$("tr:last")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="179" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/not#selector" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:not(selector)</a></span></td><td valign="top" width="205" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">去除所有与给定选择器匹配的元素</td><td valign="top" width="388" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找所有未选中的 input 元素: $("input:not(:checked)")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="181" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/even" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:even</a></span></td><td valign="top" width="205" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">匹配所有索引值为偶数的元素,从 0 开始计数</td><td valign="top" width="387" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找表格的1、3、5...行:$("tr:even")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="182" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/odd" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:odd</a></span></td><td valign="top" width="205" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">匹配所有索引值为奇数的元素,从 0 开始计数</td><td valign="top" width="387" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找表格的2、4、6行:$("tr:odd")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="182" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/eq#index" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:eq(index)</a></span></td><td valign="top" width="205" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">匹配一个给定索引值的元素 <br style="margin:0px; padding:0px"/>注:index从 0 开始计数</td><td valign="top" width="387" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找第二行:$("tr:eq(1)")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="182" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/gt#index" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:gt(index)</a></span></td><td valign="top" width="205" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">匹配所有大于给定索引值的元素 <br style="margin:0px; padding:0px"/>注:index从 0 开始计数</td><td valign="top" width="387" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找第二第三行,即索引值是1和2,也就是比0大:$("tr:gt(0)")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="182" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/lt#index" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:lt(index)</a></span></td><td valign="top" width="205" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">选择结果集中索引小于 N 的 elements <br style="margin:0px; padding:0px"/>注:index从 0 开始计数</td><td valign="top" width="387" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找第一第二行,即索引值是0和1,也就是比2小:$("tr:lt(2)")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="182" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/header" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:header</a></span></td><td valign="top" width="205" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">选择所有h1,h2,h3一类的header标签.</td><td valign="top" width="387" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">给页面内所有标题加上背景色: $(":header").css("background", "#EEE");</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="182" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/animated" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:animated</a></span></td><td valign="top" width="205" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">匹配所有正在执行动画效果的元素</td><td valign="top" width="387" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">只有对不在执行动画效果的元素执行一个动画特效:<p style="margin:10px auto; padding-top:0px; padding-bottom:0px; padding-left:20px">$("#run").click(function(){ <br style="margin:0px; padding:0px"/> $("div:not(:animated)").animate({ left: "+=20" }, 1000); <br style="margin:0px; padding:0px"/>});</p></td></tr></tbody></table>
~~~
function f1(){
$("li").css("color","red"); //获得全部li元素
//过滤选择器
$("li:first").css("color","red"); //把全部li元素的第一个过滤出来
$("li:last").css("color","red"); //把全部li元素的最后一个过滤出来
$("li:odd").css("color","red"); //奇数 li,下标从0开始计算
$("li:even").css("color","red"); //偶数li,下标从0开始计算
$("li:eq(6)").css("color","red"); //过滤指定下标的节点 eq (equal)
//过滤下标大于某个范围的元素
$("li:gt(3)").css("color","red"); //gt : greate than(greate wall)
$("li:lt(4)").css("color","red"); //lt : less than
//过滤器【嵌套】使用
$("li:odd") //奇数li li的下标从0开始
$("li:gt(0):odd").css("color","red") //奇数,(智 [播])
//li:gt(0)元素下标重新从0开始计算,再与odd进行赛选
$("li:gt(2):even").css("color","red"); //”客“ 开始
$("li:gt(3):eq(2)").css("color","red"); //"集"字被过滤出来
$("li:not(#ke)").css("color","red"); //not去除指定的选择器元素
$("li:not(#ke,#yu)").css("color","red"); //not去除指定的选择器元素
//$("*:header").css("color","red");//过滤h1 h2 h3 h4 h5 h6等标题标签
//$("h2")//全部h2
$(".apple:header").css("color","red");//第二个h2
}
</script>
<style type="text/css">
</style>
</head>
<body>
<h2>我是正式标题</h2>
<h2 class="apple">简单过滤选择器</h2>
<ul class="apple">
<li>传</li>
<li>智</li>
<li>播</li>
<li id="ke">客</li>
<li>教</li>
<li id="yu">育</li>
<li>集</li>
<li>团</li>
</ul>
<input type="button" value="触发" onclick="f1()" />
</body>
~~~
### 4. 内容过滤器 Content Filters
| 名称 | 说明 | 举例 |
|-----|-----|-----|
| [:contains(text)](http://docs.jquery.com/Selectors/contains#text) | 匹配包含给定文本的元素 | 查找所有包含 "John" 的 div 元素:$("div:contains('John')") |
| [:empty](http://docs.jquery.com/Selectors/empty) | 匹配所有不包含子元素或者文本的空元素 | 查找所有不包含子元素或者文本的空元素:$("td:empty") |
| [:has(selector)](http://docs.jquery.com/Selectors/has#selector) | 匹配含有选择器所匹配的元素的元素 | 给所有包含 p 元素的 div 元素添加一个 text 类: $("div:has(p)").addClass("test"); |
| [:parent](http://docs.jquery.com/Selectors/parent) | 匹配含有子元素或者文本的元素 | 查找所有含有子元素或者文本的 td 元素:$("td:parent") |
实例:
**4.1 :contains(text)**
用法: $(”div:contains(’John’)”)
匹配包含给定文本的元素
~~~
<div>this is computer</div>
<div>I amJohn</div>
~~~
**4.2 :empty**
用法: $(”div:empty”)
匹配所有不包含子元素或者文本的空元素
~~~
<div>I am John</div>
<div></div>
<div><span></div>
<div> </div>
<div>bread</div>
~~~
**4.3 :has(selector)**
用法: $(”div:has(.milk)”)
匹配含有选择器所匹配的元素的元素
~~~
<div></div>
<div><pclass=”milk”></div>
<div><spanclass=”milk”></div>
~~~
**4.4 :parent**
用法: $(”p:parent”)
匹配含有子元素或者文本的元素(元素必须有后代)
~~~
<p></p>
<p><span></p>
<p>hello</p>
<p> </p>
~~~
### 5.可见性过滤器 Visibility Filters
<table cellspacing="1" cellpadding="5" width="783" border="1" style="margin:0px 0px 0px 10px; padding:0px; width:650px; border:1px solid silver; border-collapse:collapse; word-break:break-word; color:rgb(51,51,51); font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px"><tbody style="margin:0px; padding:0px"><tr style="margin:0px; padding:0px"><td valign="top" width="233" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">名称</span></td><td valign="top" width="235" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">说明</span></td><td valign="top" width="309" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px; color:rgb(0,0,0)">举例</span></td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="233" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; padding-left:20px"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/hidden" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:hidden</a></span></p></td><td valign="top" width="235" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; padding-left:20px">匹配所有的不可见元素</p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; padding-left:20px">注:在1.3.2版本中, hidden匹配自身或者父类在文档中不占用空间的元素.如果使用CSS visibility属性让其不显示但是占位,则不输入hidden.</p></td><td valign="top" width="309" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找所有不可见的 tr 元素:$("tr:hidden")</td></tr><tr style="margin:0px; padding:0px"><td valign="top" width="233" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"><span style="margin:0px; padding:0px"><a target="_blank" href="http://docs.jquery.com/Selectors/visible" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom-width:1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51)">:visible</a></span></td><td valign="top" width="235" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">匹配所有的可见元素</td><td valign="top" width="309" style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse">查找所有可见的 tr 元素:$("tr:visible")</td></tr></tbody></table>
元素是隐藏的:
display:none; 隐藏(不会占据物理空间)
< input type=”hidden”>隐藏域
visibility:hidden 隐藏(要占据物理空间)
:hidden 获得隐藏元素(display input type=hidden)有两种情况会起作用
:visible获得可见元素
总结
1.基本选择(id class tag标签 sel1,sel2,sel3联合 )
2. 层次选择器
3. 简单过滤选择器(:first :last :eq(下标) :gt() :lt() :odd :even)
4. 内容过滤选择器(:contains :has :empty :parent)
5. 可见性过滤选择器(:hidden :visible)