CSS中唯一具有文本同级的孩子

h b*_*bob 5 html css css-selectors css3

我想使以下内部跨度消失,如果它是独生子:

<div class="foo">
  <span></span>  
</div>
Run Code Online (Sandbox Code Playgroud)

即选择器在这种情况下不起作用:

<div class="foo">
  <span></span>  
  Some text
</div>
Run Code Online (Sandbox Code Playgroud)

我试着:only-child:last-child它不工作,我认为是因为“一些文本”的文本。

Ion*_*ula 1

我认为仅靠 CSS 是无法做到这一点的。您应该通过设定一些条件来使用 jQuery 或 JavaScript。

if ($(".foo").text().length < 1 && $('.foo span').is(':empty')) {
    //hide your element here
    $('.foo').hide();
}
Run Code Online (Sandbox Code Playgroud)