如何选择没有前导或尾随文本节点的元素? [英] How to select an element that has no leading or trailing text nodes?

查看:100
本文介绍了如何选择没有前导或尾随文本节点的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用:only-child 伪类,但不幸的是这似乎不考虑文本节点:

 < style type =text / css> 
div span:only-child {
color:red;
}
< / style>

< div>
Test
< span>这仍会变成红色:(< / span>
< / div>

< div>
< ; span>这会变成红色,因为它应该!< / span>
< / div>
< div>
< span& ; / span>
< span>这不会变成红色 - 非常好!< / span>
< / div>

我试图找到一种方法来检测特定元素在其容器元素中是否完全独立,在我无法引入新类的情况下。

$

解决方案


p>有一种方法可以用CSS实现这一点?


不幸的是,不是。



包含在 旧版本 中CSS工作组的错误列表缺少的想法..


没有裸体文本与元素混合。


当前列表



文本节点不是元素并且CSS不能选择(或忽略)不存在的元素。



因此,最好的做法是在页面中合并文本时始终使用文本元素...你永远不知道什么时候你可能需要风格。


I experimented with using the :only-child pseudo-class but unfortunately this does not seem to consider the text nodes:

<style type="text/css">
  div span:only-child {
    color: red;
  }
</style>

<div>
  Test
  <span>This still becomes red :(</span>
</div>

<div>
  <span>This becomes red, as it should!</span>
</div>
<div>
  <span>This does not become red - great!</span>
  <span>This does not become red - great!</span>
</div>

I am trying to find a way to detect when a specific element is completely alone within its container element in a situation where I am unable to introduce new classes.

Is there a way to achieve this with CSS?

解决方案

Is there a way to achieve this with CSS?

Unfortunately, not.

Included in an old revision of the CSS Working Group "mistakes" list is missing the idea that..

No naked text mixing with elements. All raw text should have an addressable, stylable element wrapping it, created by CSS if necessary.

Current list

Text Nodes are not element and CSS can't select (or ignore) elements that don't exist.

So, it's probably best practice to always use a text element when incorporating text in a page...you never know when you might need to style it.

这篇关于如何选择没有前导或尾随文本节点的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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