【问题标题】:Select a class except another class in jquery [duplicate]在jquery中选择一个除另一个类之外的类[重复]
【发布时间】:2016-05-04 10:05:21
【问题描述】:

我想选择一个具有 .handle 类但没有 .exception 类的元素-

HTML-

<div class="handle exception">  One <div>
<div class="handle">  Two <div>

$(document).on('click', '.handle', function (e) {
   //I want to select only 2nd div.
});

有什么帮助吗?

【问题讨论】:

  • .handle:not(.exception)

标签: jquery selector


【解决方案1】:

使用.not 取消选择特定类

例如

$(".handle").not(".exception")...

在你的情况下,这将在@Mohamed-Yousef 的回答中提到

【讨论】:

  • OP 需要一个选择器,可以作为第二个参数传递给.on。
【解决方案2】:

你可以使用:not选择器

$(document).on('click', '.handle:not(.exception)', function (e) {
   //I want to select only 2nd div.
});

或选择第二个 .handle 类元素,您可以使用 :eq(1) 或 nth-child(2)

'.handle:eq(1)'  // index start from 0 so 1 to select 2nd one
'.handle:nth-child(2)'  // index start from 1 so 2 to select 2nd one

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2013-07-10
    • 1970-01-01
    • 2012-02-01
    • 2011-11-09
    相关资源
    最近更新 更多