select :only-child only if there is no additional content

select :only-child only if there is no additional content

有没有办法,用cssselect或者(没有javascript),给select一个子元素仅当没有附加内容?

我想实现以下目标:

<style>p b:only-child { color: red }</style>
<p><b>this should be red</b></p>
<p><b>this</b> should be not</p>
<p><b>this</b> <i>neither</i></p>

:only-child selector works for the third line, but not for the second. See this jsfiddle.

如果您查看规范,在 structural pseudo-classes 部分下它说:

Selectors introduces the concept of structural pseudo-classes to permit selection based on extra information that lies in the document tree but cannot be represented by other simple selectors or combinators.

Standalone text and other non-element nodes are not counted when calculating the position of an element in the list of children of its parent. When calculating the position of an element in the list of children of its parent, the index numbering starts at 1.

唯一考虑文本节点存在的选择器(对于 不匹配 ,很奇怪)是 :empty。所有其他结构伪类包括:only-child不考虑它们。