CSS选择器,用于获取HTML树中特定类的最深层元素

Mat*_*man 12 css css-selectors

我的HTML中有一堆DIV元素,其中几个将其class属性设置为"rowsLayout".这些rowsLayout DIV中的一些可以彼此嵌套.我想定义一个CSS选择器,它只针对那些嵌套中最深的DIV.也就是说,我希望任何包含任何其他rowLayout DIV的rowsLayout DIV.

<div id="a" class="rowsLayout">
  <div id="b" class="rowsLayout" />
  <div id="c" class="rowsLayout">
    <div id="d" class="rowsLayout" />
  </div>
</div>
<div id="e" class="rowsLayout" />
Run Code Online (Sandbox Code Playgroud)

有了这个结构,我想要一个将b,d和e作为目标的选择器.

可以这样做吗?

SLa*_*aks 13

您可以使用jQuery选择器.rowsLayout:not(:has(.rowsLayout)).

但是,出于性能原因,这在CSS中是不可能的.

您的选择器取决于您定位的元素的(或缺少).
设计CSS是为了在元素的子元素存在之前始终可以解析元素的选择器.这允许CSS作为文档加载应用.


jac*_*Joe -1

您可以考虑向父项添加额外的类(例如“parent”)吗?这会更容易并且是“标准”