CSS没有兄弟选择器 [英] CSS no sibling selector

查看:84
本文介绍了CSS没有兄弟选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下html:

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

我对选择器(用于爬网内容,因此我无法修改html)感兴趣,该选择器将选择所有与类idontwant没有同级的iwant.

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.

推荐答案

没有同级选择器来按类匹配(或不匹配)元素.

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

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

The closest selector I can think of is

.iwant:only-child

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

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.

这篇关于CSS没有兄弟选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆