【发布时间】:2020-12-03 22:34:50
【问题描述】:
需要定位具有以depth-开头的特定类的每个元素
我尝试使用 CSS 进行定位:
[class^="depth-"] {
margin-left: 40px;
}
但它仅在目标类是类顺序中的第一个时才有效。
就我而言:
<div class="comment byuser comment-author-admin odd alt depth-2 parent" id="comment-13">
<div id="div-comment-13" class="media">
...
</div>
<div class="comment byuser comment-author-admin even depth-3" id="comment-14">
<div id="div-comment-14" class="media">
...
</div>
</div>
</div>
【问题讨论】:
-
你不能手动为你想要的元素添加一个自定义的附加类并在 CSS 中应用属性吗?
-
您唯一的其他真实选择是
[class*="depth-"]。这不是精确的,因为“深度”可以出现在任何地方而不是开始类名。 -
在 -depth 之前留一个空格可能会有所帮助
div[class*=' depth-'],div[class^='depth-'] { }