结合 CSS 属性和伪元素选择器?
如何将伪元素选择器 (:after
) 与属性选择器 ([title]
) 结合起来?
我尝试使用 div[title]:after
,但这在 Chrome 中不起作用。
HTML:
<div title="foo">This div has a title.</div>
<div>This div does not have a title.</div>
CSS:
div:after {
content: "NO";
}
div[title]:after {
content: "YES";
}
演示: http://jsfiddle.net/jmAX6/
它在每个 div 的末尾,当第二个 div 之后应该显示“NO”时。
我运行的是 Chrome 17.0.963.38。它似乎在 Safari 5.1.2 中工作正常。
How can I combine a pseudo-element selector (:after
) with an attribute selector ([title]
)?
I tried using div[title]:after
, but this doesn't work in Chrome.
HTML:
<div title="foo">This div has a title.</div>
<div>This div does not have a title.</div>
CSS:
div:after {
content: "NO";
}
div[title]:after {
content: "YES";
}
Demo: http://jsfiddle.net/jmAX6/
It shows me "YES" at the end of each of those div's, when it should show "NO" after the second div.
I'm running Chrome 17.0.963.38. It seems to work fine in Safari 5.1.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来像是一个错误,最终报告。如果您在样式表中的 任意位置 添加了一条 CSS 规则,并且至少有一个声明,则您的
div[title]:after
规则会神奇地应用。例如:请参阅更新后的小提琴。
它似乎也与您的第二个
div
有或没有某些 HTML 属性有关,如下面的评论所示。This looks like a bug, which has finally been reported. If you add a CSS rule for
div[title]
anywhere in your stylesheet with at least one declaration, yourdiv[title]:after
rule will magically apply. For example:See the updated fiddle.
It also seems to have something to do with your second
div
having or not having certain HTML attributes, as shown in the comments below.