【问题标题】:How can I select all elements without a given class in jQuery?如何在 jQuery 中选择没有给定类的所有元素?
【发布时间】:2011-01-27 17:31:08
【问题描述】:

鉴于以下情况:

<ul id="list">
    <li>Item 1</li>
    <li class="active">Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
</ul>

我怎样才能选择除第 2 项之外的所有内容,也就是类似:

$("ul#list li!active")

【问题讨论】:

  • $("ul#list").not(".active")$("ul#list:not(.active)")

标签: jquery


【解决方案1】:

您可以使用.not() 方法或:not() 选择器

基于您的示例的代码:

$("ul#list li").not(".active") // not method
$("ul#list li:not(.active)")   // not selector

【讨论】:

  • 如果要检查两个类使用.not(".completed, .current")
  • 2019 vanillaJs:document.querySelectorAll('.foo-class:not(.bar-class):not(.foobar-class'))
【解决方案2】:

$("ul#list li:not(.active)") 呢?

http://api.jquery.com/not-selector/

【讨论】:

    【解决方案3】:

    您可以使用它来选择所有没有类的 li 元素:

    $('ul#list li:not([class])')
    

    【讨论】:

      【解决方案4】:

      请参阅 jQuery API 文档: not() selectornot equal selector

      【讨论】:

        【解决方案5】:
        if (!$(row).hasClass("changed")) {
            // do your stuff
        }
        

        【讨论】:

          猜你喜欢
          • 2011-02-07
          • 2017-12-11
          • 1970-01-01
          • 2011-02-02
          • 1970-01-01
          • 2020-05-21
          • 1970-01-01
          • 2011-11-16
          • 1970-01-01
          相关资源
          最近更新 更多