简体   繁体   English

querySelectorAll和.not()

[英]querySelectorAll and .not()

<div class="alpha">
   <div class="beta">
      <label style="color:red; font-size: 12px;">Hello</label>
   </div>
</div>

I want to exclude (= not include), the above label . 我想排除(=不包括)上面的label This includes it (tested successfully): 包括它(已成功测试):

document.querySelectorAll(.alpha .beta label");

Now how to exclude it? 现在如何排除它? I want to select all my label class but this one I want to exclude only has a label style . 我想选择所有label class但是我要排除的这一label class仅具有label style

You cannot use CSS selectors to match " all label s except the one contained in div.alpha div.beta ". 您不能使用CSS选择器来匹配“ div.alpha div.beta 包含的所有label所有label ”。 However, you gave an alternative definition of your expected results: 但是,您为预期结果提供了另一种定义:

I want to select all my <label class=…> but this one I want to exclude only has a <label style=…> . 我想选择所有我的<label class=…>但是我要排除的那一个只有<label style=…>

You can do that using an attribute selector : 您可以使用属性选择器

document.querySelectorAll("label[class]:not([style])")

Try this: 尝试这个:

document.querySelectorAll("*:not(.div_label):not(.alpha):not(.beta)");

You don't need that last 'label' part, as label is not a div (and you are asking for div to get selected, right?), so it wont get selected anyway 您不需要最后一个“标签”部分,因为标签不是div(并且您要选择div,对吗?),所以无论如何它都不会被选中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM