jQuery: Select an element only when it has no class name

前端 未结 4 1694
轻奢々
轻奢々 2021-02-13 14:39

How can I use jQuery to select an element only if it doesn\'t have any classes?

I\'m writing a page which allows the html author to override the default jQuery action by

相关标签:
4条回答
  • 2021-02-13 14:53

    This should do the thing: $('li').not('li[class]')

    0 讨论(0)
  • 2021-02-13 14:54

    :not() Selector – jQuery API

    $('li:not([class])')
    
    0 讨论(0)
  • 2021-02-13 15:01

    You can use this:

    if (!jQuery('li').hasClass('myClass') {
        do something...
    }
    
    0 讨论(0)
  • 2021-02-13 15:07

    This should do it:

    $('li:not([class]),li[class=""]')
    

    Demo: http://jsfiddle.net/aMC5X/

    The first part, li:not([class]) uses the "not selector" with the "has attribute" to find any li elements that don't have the class attribute at all.

    The second part, li[class=""] finds any li elements that have the attribute set to an empty string.

    0 讨论(0)
提交回复
热议问题