如何通过一个类查找元素,但使用JQuery排除其他元素 [英] How to find elements by one class, but exclude other using JQuery

查看:224
本文介绍了如何通过一个类查找元素,但使用JQuery排除其他元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个具有类似类的元素

I have multiple elements with a class like

<li class="target-class exclude-class"></li>
<li class="target-class exclude-class"></li>
<li class="target-class"></li>
<li class="target-class"></li>
<li class="target-class"></li>

仅当目标类没有排除类时,我才想找到它们.我尝试过

I would like to find elements with target-class only if they dont have exclude-class. I tried

var elements = $find('.target-class exclude-class:not');

但是我掌握了所有要素

推荐答案

您没有正确使用:not选择器. 您可以使用 方法选择此方案的元素或 :不 选择器.

You are not using the :not selector correctly. You can select the elements for this scenario either using the not method or the :not selector.

  $('.target-class:not(.exclude-class)');  // :not selector

$('.target-class').not('.exclude-class'); // .not method

检查小提琴

Check Fiddle

这篇关于如何通过一个类查找元素,但使用JQuery排除其他元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆