小编Jos*_*son的帖子

是否有第一个孩子的CSS选择器,考虑文本节点?

我正在尝试为第一个子元素找到一个CSS选择器,考虑任何可能在它之前的文本节点(即如果之前有任何元素,可能是未包装的文本节点,这不再被认为是第一个孩子).

但它似乎:first-child不包括文本节点,也不包括:nth-child等等.

这就是我所处的位置,但它不起作用:

.red-if-not-first {
  color: red;
  font-weight: bold;
}

.red-if-not-first:first-child {
  color: green;
}
Run Code Online (Sandbox Code Playgroud)
<p>
  Lorem ipsum. <span class="red-if-not-first">This should be red, not green, because some content comes before it.</span> Eum natus culpa officia a molestias, sed beatae aut in autem architecto iure repellat quam placeat, expedita maxime laborum necessitatibus repudiandae. Corrupti!
</p>

<p>
  <span class="red-if-not-first">This is rightly green, not red, because it's first bit of content in this paragraph.</span> Lorem ipsum dolor sit amet, …
Run Code Online (Sandbox Code Playgroud)

css

8
推荐指数
3
解决办法
1403
查看次数

标签 统计

css ×1