加载中...

:not(selector)


概述    not selector

返回值:Array<Element(s)>

描述:选择所有元素去除不匹配给定的选择器的元素。

  • V : 1.0jQuery( ":not(selector)" )

    selector: 一个用来过滤的选择器。

所有的选择器可以放置在 :not()中,例如 :not(div a):not(div,a)

Additional Notes(其他注意事项):

.not()方法可以让代码更易读。而使用 :not() 通常会构建出一个非常复杂的选择器。所以大多数情况下,推荐使用 .not()方法。

示例

实例

查找所有未选中的 input 元素

HTML 代码:
  1. <input name="apple" />
  2. <input name="flower" checked="checked" />
jQuery 代码:
  1. $("input:not(:checked)")
结果:
  1. [ <input name="apple" /> ]

实例

查找所有没有被选中的复选框,然后高亮后面的 span。注意,当你点击复选框的时候不会有反应,因为没有绑定任何点击事件。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>not demo</title>
  6. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  7. </head>
  8. <body>
  9. <div>
  10. <input type="checkbox" name="a">
  11. <span>Mary</span>
  12. </div>
  13. <div>
  14. <input type="checkbox" name="b">
  15. <span>lcm</span>
  16. </div>
  17. <div>
  18. <input type="checkbox" name="c" checked="checked">
  19. <span>Peter</span>
  20. </div>
  21. <script>
  22. $( "input:not(:checked) + span" ).css( "background-color", "yellow" );
  23. $( "input").attr( "disabled", "disabled" );
  24. </script>
  25. </body>
  26. </html>

运行一下


还没有评论.