选择recursive:last-child。可能? [英] Select recursive :last-child. Possible?

查看:169
本文介绍了选择recursive:last-child。可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CSS中,是否可以递归地从body中递归选择所有:last-child?

In CSS, is it possible to recursively select all :last-child from body?

给定此标记:

<body>
  <div id="_1">
    <div id="_2"></div>
  </div>
  <div id="_3">
    <div id="_4">
      <div id="_5"></div>
      <div id="_6"></div>
    </div>
  </div>
</body>

我在找div。 3,4和6

I am looking for div no. 3, 4 and 6

另一种表达方式是:

body > :last-child,
body > :last-child > :last-child,
body > :last-child > :last-child > :last-child,
body > :last-child > :last-child > :last-child > :last-child {
  /* My stuff here */
}

但显然这不是一个好方法。

But obviously this is not a good approach.

推荐答案

不,不幸的是,这只是一个方法,

No, unfortunately that's just about the only way to do it without modifying the HTML.

已经有针对:first-child :last-child c>伪类,但它似乎没有得到太多的青睐。注意它建议以与您的问题相同的方式嵌套和重复伪类:

There has been at least one request for recursive versions of the :first-child and :last-child pseudo-classes, but it doesn't seem to have gained much favor. Notice it suggests nesting and repeating the pseudo-classes in the same way as in your question:


目前,AFAIK,我们只能匹配子类(

Currently, AFAIK, we can only match children up to some exact nesting level known in advance (3 in the example below):

.container > :first-child,
.container > :first-child > :first-child,
.container > :first-child > :first-child > :first-child {}



我们不能只使用:first-child上下文选择器,

We cannot use just :first-child context selector since it would also select first children of blocks that are not first children themselves.

因此,我们需要一种排序的递归选择器,它不仅匹配最后一个子元素的第一个子元素,但会递归匹配所有最前和最后的元素,而不考虑它们的嵌套级别。

So we need a sort of recursive selector that matches not just first of last child, but recursively matches all first-most and last-most elements regardless of their nesting level.

这篇关于选择recursive:last-child。可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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