rzb*_*rzb 13 css css-selectors
我想选择一个不包含以以下开头的类的子元素z-depth-
:
<div class="well">
<div class="well"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
因此,如果内部.well也包含类似z-depth-1
它不会被选中的类.
这是行不通的,因为内部.well总是被选中:
.well .well:not([class^="z-depth-"])
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?
Ser*_*sov 14
您不能 选择不包含z-depth-
以CSS 开头的类的子元素,您只能:
class
属性值不从z-depth-
子字符串开始的所有子元素:.well .well:not([class^="z-depth-"]) {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
<div class="well z-depth-1">Parent div
<div class="z-depth-2 well">First child div</div>
<div class="well z-depth-3">Second child div</div>
</div>
Run Code Online (Sandbox Code Playgroud)
class
属性值不包含z-depth-
子字符串的所有子元素:.well .well:not([class*="z-depth-"]) {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
<div class="well z-depth-1">Parent div
<div class="z-depth-2 well">First child div</div>
<div class="well z-depth-3">Second child div</div>
<div class="well">Third child div</div>
</div>
Run Code Online (Sandbox Code Playgroud)
您还可以阅读有关MDN上所有CSS选择器的更多信息.
您需要结合^=
和*=
才能获得所需的结果。
.well:not([class^="z-depth-"]) { /*will ignore elements if the first class is z-depth-* */
background-color: lightgreen;
}
.well:not([class*=" z-depth-"]) { /*will ignore elements if z-depth-* is second class or later */
background-color: skyblue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="z-depth-1 well">z-depth-1 well</div>
<div class="well z-depth-1">well z-depth-1</div>
Run Code Online (Sandbox Code Playgroud)
这是关于如何使用属性选择器的很好的指南。