css - :empty selector not working in css

标签 css css-selectors

我在 .error 类中使用了 :empty 选择器。问题是,即使 div 中没有包含错误类的内容,错误类也不会完全删除。 当我在 firebug 中测试时,我发现 div 仍然有一些空白,当我删除多余的空格时,div 就会消失。

.error{ border:solid 1px #ff0000; color:#ff0000;}
.error:empty{ display:none;}

div 在调试时显示如下:

<div class="error">     </div>

您看到的多余空格就是问题所在。 有什么办法可以在 css 中显示   以显示:无?请帮忙。

最佳答案

是因为<div class="error"> </div> ( demo ) 不为空,它有一个文本节点作为子节点。

:empty

The :empty pseudo-class represents any element that has no children at all. Only element nodes and text (including whitespace) are considered. Comments or processing instructions do not affect whether an element is considered empty or not.

尝试

<div class="error"></div>

演示:Fiddle

关于css - :empty selector not working in css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18313689/

相关文章:

css - 在 Wordpress 上修改 Youtube 订阅按钮

html - 使用多个样式表时 float 标题

css - 第 n 个 child 计算不正确

css - 为所有元素设置字体系列

java - 无需类即可从 span 获取内容?

html - 在 style 属性中使用 CSS 选择器

css - 我该如何解决这个 CSS 异常?

html - 背景图像移动响应与 Bootstrap

jquery/css 向下滑动但子元素不应移动

html - 除了一个 :first-child or aside:first-child a?