How to Select Element That Does Not have Specific Class

前端 未结 5 2051
别跟我提以往
别跟我提以往 2020-11-27 14:38

I\'m wondering how to select an element that does not have a specific class using JavaScript, not jQuery.

For example, I have this list:

5条回答
  •  误落风尘
    2020-11-27 15:02

    This selects the second LI element.

    document.querySelector("li:not([class])")
    

    or

    document.querySelector("li:not(.completed):not(.selected)")
    

    Example:

    // select li which doesn't have a 'class' attribute...
    console.log(document.querySelector("li:not([class])"))
    
    // select li which doesn't have a '.completed' and a '.selected' class...
    console.log(document.querySelector("li:not(.completed):not(.selected)"))
     
    • One Task
    • Two Task

提交回复
热议问题