jQuery not() 方法用于移除匹配条件的元素,它与 filter() 方法相反。

语法

 $(selector).not(criteria,function(index)) 
  • criteria: 它是一个可选参数。它可以是 jQuery 对象或要从元素集中删除的以逗号分隔的元素列表。它是一个选择器表达式,可以是特定元素的 id 和 class。
  • function: 也是一个可选参数。此参数指定为组中的每个元素运行的函数。如果函数返回 false,则保留元素。否则,返回 true 时,元素被移除。
    • index 参数表示元素在集合中的位置。它从 0 位置开始。

例子

<!DOCTYPE html>
<html>
<head>
<style>
div{
font-size: 20px;
font-weight: bold;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
function fun(){
$(document).ready(function(){
  $("p").not(".para").css({"background": "yellow"});
});
}
</script>
</head>
<body>
<h2> 欢迎来到 yxjc123.com </h2>
<h4> 这是一个使用 jQuery 的 not() 方法的例子。 </h4>
<div id = "div1"> 这是第一个 div 元素。 </div>
<p class = "para"> P1 class = "para" </p>
<div id = "div2"> 这是第二个 div 元素 </div>
<p> P2 </p>
<p> P3</p>
<p class = "para"> P4 class = "para" 点击以下按钮查看效果。 </p>
<button onclick = "fun()"> 点击 </button>
  </body>
</html>
效果如下

jQuery not() 方法