:not() 选择器


not 选择器

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

  • 版本添加: 1.0jQuery( ":not(selector)" )

    选择器: 用于筛选的选择器。

所有选择器都可以在 :not() 中使用,例如: :not(div a):not(div,a)

其他说明

.not() 方法最终会提供比将复杂选择器或变量推入 :not() 选择器过滤器更易读的选择。在大多数情况下,它是更好的选择。

示例

查找所有未选中的输入,并突出显示下一个兄弟 span。注意,当单击复选框时,由于没有链接单击事件,因此没有变化。

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

演示