jQuery选择器过滤器基础大全

1. 基本选择器

  1. 元素选择器$('div')
  2. ID选择器$('#first')
  3. 类选择器$('.second')
  4. 通配符选择器$('*')

2. 多项选择器

$('#idsector div .classSector') 不会重复选择 不会改变元素在html中的顺序(三种选择结果合并)

3. 层级选择器

  1. 祖先后代选择器$('ancestor descentant')(孙子也会被选中)
  2. 直接后代选择器$('parent > child')
  3. 相邻选择器$('prev + next')选中next前面是prev的next
  4. 兄弟选择器$('prev ~ siblings')prev后面的所有同层次的siblings

4. 属性选择器

  1. 属性名选择器$('[class]')
  2. 属性值选择器$('[class = one]')
  3. 非属性值选择器$('[class != one]')
  4. 开头匹配选择器$('[class ^= one]')匹配以one开头的属性
  5. 结尾匹配选择器$('[class $= one]')匹配以one结尾的属性
  6. 包含匹配选择器$('[class *= one]')匹配含有one的属性
  7. 多条件属性选择器$('[src][class *= one]')匹配同时有src且class的值中含有one的属性

5. child过滤器

  1. $('details:first-child')第一个details标签
  2. $('details > p:first-child') 第一个子标签是p的details下的第一个p标签
  3. $('details > p:nth-child(2)') 第二个子标签是p的details下的第二个p标签
  4. $('details > p:nth-last-child(2)') 倒数第二个子标签是p的details下的倒数第二个p标签
  5. $('details > p:only-child') 只有一个p作为子标签的p

6. type过滤器

  1. $('details:first-of-type')第一个details标签
  2. $('details > p:first-of-type') 子标签中有p的details标签的第一个p标签(不管p是不是第一个子元素)
  3. $('details > p:nth-of-type(2)') 至少有2个子标签是p中的第二个
  4. $('details > p:nth-last-of-type(2)') 至少有2个子标签是p中的倒数第二个
  5. $('details > p:only-of-type') 有p标签且p标签只有唯一一个

7. 过滤器参数

  1. n 2n 3n … 必须为整数 从1开始
  2. even 2n 所有偶数元素
  3. odd 2n+1 所有奇数元素
  4. formula 使用特殊公式(an+b)
    $('details > p:nth-of-type(2n)');
    

8. 表单相关

  1. $(':input') 可以选择 input textarea select button
  2. $(':text') 匹配所有单行文本框 和input[type=‘text’]一样
  3. 其他input的type :password/:radio/:checkbox/:image/:reset/:button/:file
  4. $(':enabled')$(':disabled')
  5. $(':checked')所有被选中的元素(单选框 复选框 select中的option)
  6. $(':selected') 匹配所有选中的option元素

9. 查找和过滤

  1. find 多层 children 单层
  2. parent
  3. next prev
  4. eq $('li').eq(8) 隐式迭代 找到第八个 -8为倒数第八个
  5. siblings $('.php').siblings();
  6. filter $('li').filter('.python');
    $('aside').find('.ha');
    
发布了33 篇原创文章 · 获赞 5 · 访问量 3391

猜你喜欢

转载自blog.csdn.net/ChristWTF/article/details/104092448