是否可以编写 CSS 规则来选择没有特定类的元素的第一个子元素?
示例:
<span class="普通类忽略"></span><span class="普通类忽略"></span><span class="common-class"></span><span class="common-class"></span></div>在这种情况下,我想选择没有类 ignore 的第一个 span.我试过了,但似乎没有用:
.common-class:first-child:not(.ignore) {...一些规则...}
更新:
如果我将一个名为 parent-class 的类添加到父 div,则 Jukka 建议的选择器的修改版本可以工作,除非第一个 span 具有类 >ignore 出现在第一个没有之后.上述选择器如下:
.parent-class >.common-class.ignore + .common-class:not(.ignore) {...一些规则...}
解决方案 这个问题类似于第一个元素的 CSS 选择器有类,除了第一个元素没有一个类.如前所述,:first-child:not(.ignore) 表示一个元素,它是其父元素的第一个子元素,并且没有类ignore",而不是第一个匹配其余元素的子元素选择器.
您可以将覆盖技术与我在 我对链接问题的回答,将类选择器替换为包含类选择器的 :not() 伪类:
.common-class:not(.ignore) {/* 每个没有 class .ignore 的 span,包括第一个 *
每隔三个元素设置样式?Style every third element?(每隔三个元素设置样式?)
为什么我不应该在 CSS 中使用 ID 选择器?Why shouldn#39;t I use ID selectors in CSS?(为什么我不应该在 CSS 中使用 ID 选择器?)
CSS 中的 img[class*=“align"] 是什么意思?What does img[class*=quot;alignquot;] mean in CSS?(CSS 中的 img[class*=“align] 是什么意思?)
CSS:最后一个元素CSS: Last element on line(CSS:最后一个元素)
如何仅使用 CSS(无 js)选择所有其他 div 类元素How do I select every other div class element using just CSS (no js)(如何仅使用 CSS(无 js)选择所有其他 div 类元素)
检查未使用的 CSS 选择器的工具?Tool for checking unused CSS selectors?(检查未使用的 CSS 选择器的工具?)