1. 基本选择器
- 元素选择器
$('div')
- ID选择器
$('#first')
- 类选择器
$('.second')
- 通配符选择器
$('*')
2. 多项选择器
$('#idsector div .classSector')
不会重复选择 不会改变元素在html中的顺序(三种选择结果合并)
3. 层级选择器
- 祖先后代选择器
$('ancestor descentant')
(孙子也会被选中) - 直接后代选择器
$('parent > child')
- 相邻选择器
$('prev + next')
选中next前面是prev的next - 兄弟选择器
$('prev ~ siblings')
prev后面的所有同层次的siblings
4. 属性选择器
- 属性名选择器
$('[class]')
- 属性值选择器
$('[class = one]')
- 非属性值选择器
$('[class != one]')
- 开头匹配选择器
$('[class ^= one]')
匹配以one开头的属性 - 结尾匹配选择器
$('[class $= one]')
匹配以one结尾的属性 - 包含匹配选择器
$('[class *= one]')
匹配含有one的属性 - 多条件属性选择器
$('[src][class *= one]')
匹配同时有src且class的值中含有one的属性
5. child过滤器
$('details:first-child')
第一个details标签$('details > p:first-child')
第一个子标签是p的details下的第一个p标签$('details > p:nth-child(2)')
第二个子标签是p的details下的第二个p标签$('details > p:nth-last-child(2)')
倒数第二个子标签是p的details下的倒数第二个p标签$('details > p:only-child')
只有一个p作为子标签的p
6. type过滤器
$('details:first-of-type')
第一个details标签$('details > p:first-of-type')
子标签中有p的details标签的第一个p标签(不管p是不是第一个子元素)$('details > p:nth-of-type(2)')
至少有2个子标签是p中的第二个$('details > p:nth-last-of-type(2)')
至少有2个子标签是p中的倒数第二个$('details > p:only-of-type')
有p标签且p标签只有唯一一个
7. 过滤器参数
- n 2n 3n … 必须为整数 从1开始
- even 2n 所有偶数元素
- odd 2n+1 所有奇数元素
- formula 使用特殊公式(an+b)
$('details > p:nth-of-type(2n)');
8. 表单相关
$(':input')
可以选择 input textarea select button$(':text')
匹配所有单行文本框 和input[type=‘text’]一样- 其他input的type :password/:radio/:checkbox/:image/:reset/:button/:file
$(':enabled')
和$(':disabled')
$(':checked')
所有被选中的元素(单选框 复选框 select中的option)$(':selected')
匹配所有选中的option元素
9. 查找和过滤
- find 多层 children 单层
- parent
- next prev
- eq
$('li').eq(8)
隐式迭代 找到第八个 -8为倒数第八个 - siblings
$('.php').siblings();
- filter
$('li').filter('.python');
$('aside').find('.ha');