结合 CSS 属性和伪元素选择器?

发布于 2024-12-28 05:36:13 字数 601 浏览 0 评论 0原文

如何将伪元素选择器 (: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2025-01-04 05:36:13

这看起来像是一个错误,最终报告。如果您在样式表中的 任意位置 添加了一条 CSS 规则,并且至少有一个声明,则您的 div[title]:after 规则会神奇地应用。例如:

div:after {
    content: "NO";
}
div[title] {
    display: block;
}
div[title]:after {
    content: "YES";
}

请参阅更新后的小提琴

它似乎也与您的第二个 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, your div[title]:after rule will magically apply. For example:

div:after {
    content: "NO";
}
div[title] {
    display: block;
}
div[title]:after {
    content: "YES";
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文