Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

Jquery not()方法

not()方法返回与指定条件不匹配的元素。如果元素不符合条件,则从选择中返回它们,而匹配的元素将被删除。
这是jQuery的内置方法,与 filter()方法。假设我们有一个 jQuery 对象,它表示元素集,因此 not()方法构造一个新的jQuery对象,其中包含匹配元素的子集。传递的参数 条件 功能(索引) 将针对每个元素进行测试,并且

语法

使用条件
$(selector).not(criteria)
使用功能
$(selector).not(function(index))
此函数的参数值定义如下。
criteria: 。它是可选参数。它可以是jQuery对象,也可以是用逗号分隔的元素列表,将从元素集中删除。它是一个选择器表达式,可以是特定元素的 id class
function: 它也是可选参数。此参数指定为组中的每个元素运行的函数。如果函数返回false,则保留该元素。否则,返回true时,将删除该元素。
index 参数表示该元素在集合中的位置。它从 0 位置开始。
让我们看一些示例,以了解如何使用 not()方法。
示例1
在此示例中,我们使用 not()函数的 条件属性。在这里, not()函数返回与类名 para 不匹配的所有段落元素。有一些div元素,段落元素等。在与类 para 相关的四个段落中,有两个段落元素。
我们必须单击给定的按钮才能看到结果。
<!DOCTYPE html>
<html>
<head>
<style>
div{
font-size: 20px;
font-weight: bold;
}
</style>
<script src="/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function fun(){
$(document).ready(function(){
  $("p").not(".para").css({"background": "yellow"});
});
}
</script>
</head>
<body>
<h2> Welcome to the lidihuo.com </h2>
<h4> This is an example of using the jQuery's not() method. </h4>
  <div id = "div1"> This is first div element. </div>
          <p class = "para"> P1 with class = "para" </p>
<div id = "div2"> This is second div element. </div>
<p> P2. </p>
<p> P3. </p>
<p class = "para"> P4 with class = "para". Click the following button to see the effect. </p>
<button onclick = "fun()"> click me </button>
  </body>
</html>
输出

Welcome to the lidihuo.com

This is an example of using the jQuery's not() method.

This is first div element.

P1 with class = "para"

This is second div element.

P2.

P3.

P4 with class = "para". Click the following button to see the effect.

Example2

在此示例中,我们使用 not()函数的 function(index) 参数。此处,该函数突出显示与索引位置 1不匹配的段落元素 2
我们要记住, 索引 0 位置开头。
<!DOCTYPE html>
<html>
<head>
<style>
div{
font-size: 20px;
font-weight: bold;
}
</style>
<script src="/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function fun(){
$(document).ready(function(){
  $("p").not(function(index) {
  if(index == 1 || index == 2){
  return true;
  }
  }).css({"background": "yellow"});
});
}
</script>
</head>
<body>
<h2> Welcome to the lidihuo.com </h2>
<h4> This is an example of using the jQuery's not() method. </h4>
  <div id = "div1"> This is first div element. </div>
          <p class = "para"> P1 with class = "para" </p>
<div id = "div2"> This is second div element. </div>
<p> P2. </p>
<p> P3. </p>
<p class = "para"> P4 with class = "para". Click the following button to see the effect. </p>
<button onclick = "fun()"> click me </button>
  </body>
</html>
输出

Welcome to the lidihuo.com

This is an example of using the jQuery's not() method.

This is first div element.

P1 with class = "para"

This is second div element.

P2.

P3.

P4 with class = "para". Click the following button to see the effect.

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4