CSS 没有同级选择器

发布于 2024-12-11 20:40:44 字数 279 浏览 0 评论 0 原文

考虑以下 html:

<div>
    <div class="iwant" />
    <div class="idontwant" />
</div>
 <div>
    <div class="iwant" />
</div>

我对一个选择器感兴趣(用于抓取内容,因此我无法修改 html),它将选择所有不具有类 idontwantiwant >。

Consider the following html:

<div>
    <div class="iwant" />
    <div class="idontwant" />
</div>
 <div>
    <div class="iwant" />
</div>

I'm interested in a selector (for crawling content so I can't modify html) that would select all iwant that DO NOT have sibling with class idontwant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌若浮生 2024-12-18 20:40:44

没有同级选择器可以按类匹配(或不匹配)元素。

我能想到的最接近的选择器是

.iwant:only-child

但是这个选择器意味着除了 div class="iwant" 之外不能有任何其他元素作为父 div 的子元素,无论类型或类别。不过,这可能会满足您的需求,具体取决于 HTML 的结构,因此值得一试。但是,如果类名对您来说是个问题,那么可能没有太多解决方案,因为 CSS 中没有按类过滤的 :only-of-class 伪类并忽略其余部分。

There is no sibling selector to match elements (or not) by class.

The closest selector I can think of is

.iwant:only-child

But this selector means that there cannot be any other elements besides that div class="iwant" as children of the parent div, regardless of type or class. This may fulfill your need depending on the structure of your HTML though, so it's worth a try. If class names are a problem for you, though, then there probably isn't much of a solution, because there isn't an :only-of-class pseudo-class in CSS which filters by class and ignores the rest.

池予 2024-12-18 20:40:44

如果有人仍然需要这个,我找到了这个答案。基于它和:不是伪类,初始要求可以使用类似的方法来完成:

div:not(:has(+ .idontwant)) .iwant { 
  ... 
}

这种方法比一般的同级组合器有一个优势:它还向后匹配,意思是这样的:(

<div>
    <div class="idontwant" />
    <div class="iwant" />
</div>

所以如果你首先有 .idontwant 元素 -> case这将被一般兄弟组合器忽略)


说明:

  1. div:has(+ .idontwant) 将匹配任何与 idontwant 类具有直接同级的 div
  2. div:not (:has(+ .idontwant)) 匹配任何与 idontwant 类没有直接兄弟的 div
  3. 剩下要做的就是在没有的div与我们想要的类的 idontwant 类的直接同级。

选择器非常奇怪而且很大,但它完成了工作,而且我认为在某些(特定)情况下它是非常需要的。


编辑(来自评论):
如果有人需要仅在元素具有特定同级(或没有)时对其进行样式设置,您仍然可以使用 :有伪类来完成它(检查代码片段):

const toogleHasClassEl = document.getElementById("toogleHasClass")
toogleHasClassEl.onclick = () => toogleHasClassEl.classList.toggle("specificclass")
div:has(.specificclass) .stylethis { 
  background: orange
}
div:not(:has(.specificclass)) .stylethis { 
  background: yellow
}


div:has(.stylethis) .specificclass { 
  background: red
}
<div>
  <div class="stylethis">style this</div>
    <div id="toogleHasClass" class="specificclass">if this has `specificclass` class (click to toggle)</div>
</div>
<hr/>
<div>
  <div class="stylethis">style this different</div>
  <div class="thisdoesnthaveclass">if this doesn't have specific class</div>
</div>
<hr/>
<div>
  <div class="stylethis">style this different (if alone and therefore doesn't have specific sibling)</div>
</div>

If anyone still needs this, I found this answer . Based on it and on :not pseudo-class, the initial requirement can be accomplished using something like:

div:not(:has(+ .idontwant)) .iwant { 
  ... 
}

This approach has an advantage over the general sibling combinator: It also matches backwards, meaning something like:

<div>
    <div class="idontwant" />
    <div class="iwant" />
</div>

(so if you have the .idontwant element first -> case which would be ignored by the general sibling combinator)


Explanation:

  1. div:has(+ .idontwant) would match any div that has a direct sibling with the class idontwant
  2. div:not(:has(+ .idontwant)) matches any div that doesn't have a direct sibling with class idontwant
  3. All is left to do is search in the div that doesn't have a direct sibling with the class idontwant for the class we want.

The selector is quite weird and big, but it does the job, and I think there are (specific) cases where it is quite needed.


Edit (from comments):
If anyone needs to style an element only if it has a specific sibling (or if it doesn't) you can still use the :has Pseudo-class to accomplish it (check snippet):

const toogleHasClassEl = document.getElementById("toogleHasClass")
toogleHasClassEl.onclick = () => toogleHasClassEl.classList.toggle("specificclass")
div:has(.specificclass) .stylethis { 
  background: orange
}
div:not(:has(.specificclass)) .stylethis { 
  background: yellow
}


div:has(.stylethis) .specificclass { 
  background: red
}
<div>
  <div class="stylethis">style this</div>
    <div id="toogleHasClass" class="specificclass">if this has `specificclass` class (click to toggle)</div>
</div>
<hr/>
<div>
  <div class="stylethis">style this different</div>
  <div class="thisdoesnthaveclass">if this doesn't have specific class</div>
</div>
<hr/>
<div>
  <div class="stylethis">style this different (if alone and therefore doesn't have specific sibling)</div>
</div>

陌若浮生 2024-12-18 20:40:44

没有负同级 CSS 选择器。使用同级选择器设置新样式,然后在 .idontwant 处重置样式:

div.iwant {
    /*Set CSS here*/
    display: inline;
}
div.iwant ~ div.idontwant {
    /*Reset CSS*/
    display: block /* Default of DIV is block*/
}

There's no negative-sibling CSS selector. Use the sibling selector to set new styles, then reset the styles at the .idontwant:

div.iwant {
    /*Set CSS here*/
    display: inline;
}
div.iwant ~ div.idontwant {
    /*Reset CSS*/
    display: block /* Default of DIV is block*/
}
倦话 2024-12-18 20:40:44

如果没有特定的同级项目,就没有好的方法来定位项目。但您可以使用 .iwant:last-child 来定位没有后续同级的项目。

示例: http://codepen.io/martinkrulltott/pen/YyGgde

There's no good way to target an item without a specific sibling item. But you can use .iwant:last-child to target items that doesn't have a subsequent sibling.

Example: http://codepen.io/martinkrulltott/pen/YyGgde

夏末染殇 2024-12-18 20:40:44

如果您想要的元素始终是最后一个子元素,这也可能会有所帮助:

div:last-child:not(.idontwant) {

}

This also might be helpful - if the element you want is always last-child:

div:last-child:not(.idontwant) {

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文