<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>:not(selector)</title>
</head>
<script src="jquery-3.1.1.js">
</script>
<body>
<!--
去除所有与给定选择器匹配的元素
在jQuery 1.3中,已经支持复杂选择器了(例如:not(div a) 和 :not(div,a))
-->
<input name="apple" />
<input name="flower" checked="checked" id="a" />
<div>divdiv</div>
<a>aaa</a>
</body>
<script type="text/javascript">
//$("input:not(:checked)").css('background','purple');
//显示结果不正确[测试好多次,用了几个库都不能执行正确结果,不知道自己的问题还是什么原因,加了id之后,用id做选择器是好用的,如下]
//$("input:not(#a)").css('background','purple');显示结果正确
$(":not('input')").css('background','#666');
</script>
</html>

本文通过实例演示了如何使用 jQuery 中的 :not() 选择器来排除特定元素,并对其它元素应用样式。测试中发现使用 :not(':checked') 无法正常工作,但使用 id 作为选择器时可以正确应用背景色。
426

被折叠的 条评论
为什么被折叠?



