您的位置:首页 > Web前端 > JQuery

css和jquery选择器

2020-07-14 05:49 579 查看

jQuery选择器

基本

ID选择器(#):
类选择器(.):
通配符(*):
元素选择器(element)
并集选择器(,):#div1,#div2
交集选择器(无):#div1#div2

层级

后代(空格):所有后代元素
子代(>):所有子元素
相邻的下一个兄弟元素(+):ul+div(ul相邻的下一个元素且为div)
后面的所有兄弟元素(~):ul~div(ul后面的所有兄弟元素且为div)

筛选

:first:获取第一个元素
:last:获取最后一个元素
:not(selector):
:even:index为偶数(从0开始)
:odd:index为奇数(从0开始)
:eq(index):等于index(从0开始)
:gt(index):大于index(从0开始)
:lt(index):小于index(从0开始)
:header:匹配h1,h2,h3之类的标题元素
:animated:在、匹配正在执行动画效果的元素
:focus:匹配获取焦点的元素
:root:选择文档的根元素
:target
:lang

内容

:empty:不包含子元素或者文本
:parent:含有子元素或者文本
:has(selector):包含选择器匹配元素的
:contains(text):包含text文本的

可见性

:visible
:hidden

属性

[attr]:包含给定属性
[attr=val]:attr等于xxx的
[attr!=val]:attr不等于xxx的
[attr*=val]:attr包含xxx的
[attr^=val]:attr以xxx开始的
[attr$=val]:attr以xxx结束的
[attrsel1][attrsel2][attrseln]:多个属性选择器同时满足。[name=‘lin’][type=‘text’][value*=‘xxx’]

子元素

:first-child
:last-child
:nth-child
:nth-last-child
:only-child
:first-of-type
:last-of-type
:nth-of-type
:nth-last-of-type
:only-of-type

表单

:input:匹配所有 input, textarea, select 和 button 元素
:radio:匹配所有单选按钮。<input type=“radio” />
:checkbox:匹配所有复选框。<input type=“checkbox” />
:file:匹配所有文件域。<input type=“file” />
:text:匹配所有的单行文本框。<input type=“text” />
:password:匹配所有密码框。<input type=“password” />
:image:匹配所有图像域。<input type=“image” />
:button:匹配所有按钮。<input type=“button” /><button type=“submit” />
:submit:匹配所有提交按钮,理论上只匹配 type=“submit” 的input或者button,但是现在的很多浏览器,button元素默认的type即为submit,所以很多情况下,不设置type的button也会成为筛选结果。
:reset:匹配所有重置按钮。<button type=“reset” />

表单对象属性

:enabled:匹配所有可用元素
:disabled:匹配所有不可用元素
:selected:select中的option中被选中的
:checked:(复选框、单选框等,select中的option)中被选中的

混淆

$.escapeSelector(selector):这个方法通常被用在类选择器或者ID选择器中包含一些CSS特殊字符的时候,这个方法基本上与CSS中CSS.escape()方法类似,唯一的区别是jquery中的这个方法支持所有浏览器。
<div class=".box">span class=".box"</div>
$( “div” ).find( “.” + $.escapeSelector( “.box” ) );

css选择器

# . * element , 无
空格 > + ~
[attr] [attr=val] [attr!=val] [attr^=val] [attr$=val] [attr~=val] [attr*=val]
:link :visited :hover :active :focus
:first-line :first-letter :before :after :root :empty :target :not
:first-child :last-child :only-child :nth-child :nth-last-child
:first-of-type :last-of-type :only-of-type :nth-of-type :nth-last-of-type
:enabled :disabled :checked ::selection 被用户选中或处于高亮状态的部分
:in-range :out-of-range :read-write :read-only :required
:optional 可选的输入元素 :valid 输入值为合法的元素 :invalid 输入值为非法

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: