【jQuery】学习整理(二) 选择器
原文链接http://www.bujiaban.net/wordpress/?p=34
jQuery 的选择器分为基本选择器,层次选择器,过滤选择器,表单选择器。
选择器
神马是选择器
通过指定的规则获得符合条件的元素集合的方法。
jQuery继承了CSS选择器风格。支持CSS1,CSS2全部和部分的CSS3选择器。
基本选择器
#id 指定Id
.class 指定类名
element 指定标签名
* 所有元素
selector1,selector2.... 返回各个选择器的结果集的合并运算
层次选择器
"ancestor descendant" 选中ancestor里面的所有后代元素 如$("div span")
"parent>child" 选中parent元素下的子元素 如$("div >span")
"prev+next" 选中紧接在prev元素后的next元素 如$(".one + div") 与next方法等价
"prev~sibling" 选中prev元素之后的所有sibling元素 如$("#two~div") 与nextAll方法等价
过滤选择器
基本
:first 第一个元素 如$("div:first")
:last 选中最后一个元素 如$("div:last")
:not(slector) 除开与指定选择器匹配的元素 如$("input:not(.myClass)")
:even 选择索引是偶数的所有元素,索引从0开始 如$("input:even")
:odd 选择索引是奇数的所有元素 如$("input:odd")
:eq(index) 选择索引等于index的元素 如$("input:eq(1)")
:gt(index) 选择索引大于index的元素 如$("input:gt(1)")
:lt(index) 选取索引小于index的元素 $("input:lt(1)")
:header 选择所有的标题元素, h1,h2.. 如$(":header")
:animated 选择当前正在执行的动画元素 如$("div:animated")
内容
:contains(text) 选择含有文本内容为"text" 的元素
:empty 选择不含有子元素或文本的空元素
:has(selector) 选择含有选择器所匹配的元素的元素 如$("div:has(p)")
:parent 选择含有子元素或文本的元素 如$("div:parent")
可见性
:hidden 选择所有不可见元素 包括type="hidden"
:visible 选择所有可见元素 如$("div:visible")
属性
[attribute] 选择拥有此属性的元素 如$("div[id]")
[attribute=value] 选择属性值为value的元素
[attribute!=value] 选择属性值不为value的元素
[attribute^=value] 选择属性值以value开头的元素
[attribute$=value] 选择属性值以value结尾的元素
[attribute*=value] 选择属性值含有value的元素
[selector1][selector2][selector3].. 各个选择器结果交集 如$("div[id][title$='test']")
子元素
:net-child(index/even/odd/equation) 选择每个父元素下的第index个或者奇偶元素(index从1算起)
:first-child 选择每个父元素的第一个子元素 如$("ul li:first-child")
:last-child 选择每个父元素的最后一个子元素
:only-child 如果某个元素是它父元素的唯一子元素,则匹配 如$("ul li:only-child")
表单对象属性
:enabled 选择所有可用元素 如$("#form1 :enabled")
:disabled 选择所有不可用的元素
:checked 选择所有被选中的元素(单选框,单选框d)
:selected 选择所有被选中的选项元素(下拉列表)
表单选择器
#input 选择所有的<input> <textare<button>元素
#text
:password
:radio
:checkbox
:submit
:image
:reset
:button
:file
:hidden