Exclusive CSS selector

后端 未结 3 1871
夕颜
夕颜 2020-12-07 03:53

I am working on a page where the selected item among a list is characterized by NOT having a given class. Something like the following:

3条回答
  •  醉话见心
    2020-12-07 04:41

    In such case you may consider attribute selector like this:

    console.log(document.querySelectorAll('li[class="a"]').length)
    li[class="a"] {
      color:red;
    }
    • Select me
    • Don't select me
    • Don't select me
    • Select me

    Simply pay attention to extra spaces:

    console.log(document.querySelectorAll('li[class="a"]').length)
    li[class="a"] {
      color:red;
    }
    • Pay attention to this one !!
    • Don't select me
    • Select me

    But since you are using JS you can use trim() to get rid of the extra spaces:

    var elem=document.querySelectorAll('li');
    for(var i=0;i
    li[class="a"] {
      color:red;
    }
    • Pay attention to this one !!
    • Don't select me
    • Select me

提交回复
热议问题