jQuery API .not()

本文介绍了如何使用jQuery的.not()方法从匹配元素集合中移除指定的元素,提供了实例代码和效果演示,涵盖了不同参数的用法,包括选择器、元素对象和函数测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.not()

描述: 从匹配的元素集合中移除指定的元素。

  • .not( selector )
    • selector
      类型:   Selector
      一个用于匹配元素的选择器字符串。
  • .not( elements )
    • elements
      类型:   Elements
      要从匹配元素集合中移除的一个或多个DOM元素。
  • .not( function(index) )
    • function(index)
      类型:   Function()
      一个函数用作测试集合中的每个元素。 this  是当前DOM元素。
  • .not( jQuery object )
    • jQuery object
      类型:   PlainObject
      现有匹配当前元素集合的jQuery对象。
如果提供的jQuery对象代表了一组DOM元素, .not() 方法构建一个新的匹配元素的jQuery对象,用于存放筛选后的元素。所提供的选择器是对每个元素进行测试;如果元素不匹配的选择将包括在结果中

例子:

Example: 为不是绿色或蓝色的 div 添加边框。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script src="jquery-1.10.2.js"></script>
<style>
div { width:50px; height:50px; margin:10px; float:left;
 background:yellow; border:2px solid white; }
.green { background:#8f8; }
.gray { background:#ccc; }
#blueone { background:#99f; }
</style>
</head>

<body>
 <div></div>
<div id="blueone"></div>
<div></div>
<div class="green"></div>
 
<div class="green"></div>
<div class="gray"></div>
<div></div>
 <script>
 	$("div").not(".green,#blueone").css("border-color","red");
 </script>
 
</body>
</html>
效果图:

Example: 从段落集合中移除 ID 是 "selected" 的元素。
1
       
$("p").not( $("#selected")[0] )
Example: 从段落集合中移除 ID 是 "selected" 的元素。
1
       
$("p").not("#selected")
Example: 从段落集合中移除满足 "div p.selected" 的元素。
1
       
$("p").not($("div p.selected"))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值