.not() 遍历方法 匹配元素集合中移除元素
:not() 选择器 选取除了指定元素以外的所有元素
.siblings() 遍历方法 返回被选元素的所有同级元素

 

 

 

 

 

需排除对象单数个(1个)

 

获取ul中除  的其他所有 li

1 <ul>
2   <li>list item 1</li>
3   <li>list item 2</li>
4   <li id="unwanted">list item 3</li>
5   <li>list item 4</li>
6   <li>list item 5</li>
7 </ul>
1    $('li:not(#unwanted)').css('background', 'red');
2 
3    $('li').not('#unwanted').css('background', 'red');
4 
5   $('#unwanted').siblings().css('background', 'red');

 

需排除对象复数个(大于1个) 

获取ul中除 class="unwanted" 的其他所有 li

1 <ul>
2   <li class="unwanted">list item 1</li>
3   <li>list item 2</li>
4   <li class="unwanted">list item 3</li>
5   <li>list item 4</li>
6   <li>list item 5</li>
7 </ul>
1     $('li:not(.unwanted)').css('background', 'red');
2 
3     $('li').not('.unwanted').css('background', 'red');
4 
5     $('.unwanted').siblings().css('background', 'red');//无法达到预期效果

相关文章:

  • 2021-10-07
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2021-07-13
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2021-07-11
  • 2021-05-26
相关资源
相似解决方案