我可以使用同级组合器来定位 :before 或 :after 伪元素吗?

发布于 2024-12-09 12:22:58 字数 976 浏览 0 评论 0原文

这个 CSS 不起作用有什么原因吗?

http://jsfiddle.net/6v5BZ/

a[href^="http"]:after {
    content:"";
    width:10px;
    height:10px;
    display:inline-block;
    background-color:red;
}

a[href^="http"] img ~ :after {
    display:none;
}

..在此 HTML 上?

<a href="http://google.com">Test</a>
<a href="http://google.com">
    <img src="https://www.google.com/logos/classicplus.png">
</a>

这个想法是在匹配锚标记上有一个伪元素。但我不希望它应用于包裹图像的锚标记。因为我无法使用 a a a a a a a a a a a a a a a a < 之类的东西来定位锚点。 img,我想也许我可以通过查找其同级图像来定位 :after 伪元素。

任何见解将不胜感激。

Is there a reason why this CSS doesn't work?

http://jsfiddle.net/6v5BZ/

a[href^="http"]:after {
    content:"";
    width:10px;
    height:10px;
    display:inline-block;
    background-color:red;
}

a[href^="http"] img ~ :after {
    display:none;
}

.. on this HTML?

<a href="http://google.com">Test</a>
<a href="http://google.com">
    <img src="https://www.google.com/logos/classicplus.png">
</a>

The idea is to have a pseudo-element on matching anchor tags. But I do not want it to apply to anchor tags that wrap an image. And since I can't target anchors using something like a < img, I figured the maybe I could target the :after pseudo-element by finding an image that it's a sibling of.

Any insight would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

三生一梦 2024-12-16 12:22:58

你不能以 :after 为目标,因为它的内容没有在 DOM 中渲染,也不会对其进行操作 - 要使其工作,必须重新渲染 DOM,而 CSS 无法像这样操作它。

查看规范以了解详细信息:http://www.w3.org/ TR/CSS2/generate.html#propdef-content

生成的内容不会更改文档树。特别是,它
不会反馈到文档语言处理器(例如,对于
重新解析)。

我建议您使用 JavaScript 来完成这项工作。

You can't target :after since it's content is not rendered in the DOM and it does not manipulate it - for this to work the DOM would have to be re-rendered and CSS can't manipulate it like this.

Check the specification for detailed understanding: http://www.w3.org/TR/CSS2/generate.html#propdef-content

Generated content does not alter the document tree. In particular, it
is not fed back to the document language processor (e.g., for
reparsing).

I suggest you use JavaScript to do the job for you.

醉生梦死 2024-12-16 12:22:58

您不能使用组合器来定位相对于其生成元素以外的元素的伪元素。

这是因为它们是伪元素,而不是实际元素,并且组合器仅通过在实际元素之间建立关系来工作。另一方面,伪元素只能应用于选择器的主题(最右边的复合选择器),并且只有在对真实元素进行匹配处理后才会发生这种情况。换句话说,首先进行匹配,就好像伪元素不存在一样,然后将伪元素(如果在选择器中指示)应用于每个匹配项。

在您的代码中,以下选择器:

a[href^="http"] img ~ :after

实际上并不查找位于 aimg 之后的 :after 伪元素,即使尽管看起来是这样,因为两者都呈现为 a 元素的子元素。

它可以重写为以下内容:

a[href^="http"] img ~ *:after

注意 * 选择器,它是隐含的。与如何在任何其他简单选择器之前省略 * 以使其隐含类似,从伪元素中省略 * 也会使其隐含在那里。有关详细信息,请参阅规范

现在,即使看起来 *:after 仍然应该匹配 a:after (因为 a 会匹配 *) ,这样仍然不行。如果从选择器中删除 :after 伪元素:

a[href^="http"] img ~ *

您会注意到选择器的含义完全改变:

选择任意元素
显示为 img
的后续同级
它是 a 的后代(其 href 以“http”开头)。

由于 img 是 HTML 中 a 元素的最后一个子元素,因此没有后续同级元素可以匹配,因此没有 :after 伪元素- 可以生成元素。

对于 :before:after 伪元素,人们可能会考虑将伪元素的生成元素相对于伪元素的“同级”进行匹配,但是正如OP正确指出的那样, 没有父选择器,所以他们那里运气不好, 也。

You cannot use a combinator to target a pseudo-element relative to elements other than its generating element.

This is because they're pseudo-elements, not actual elements, and combinators only work by establishing relationships between actual elements. A pseudo-element, on the other hand, can only be applied to the subject of a selector (the rightmost compound selector), and this happens only after matching is processed on the real elements. In other words, matching is done first as though the pseudo-element wasn't there, then the pseudo-element, if it's indicated within the selector, is applied to each match.

In your code, the following selector:

a[href^="http"] img ~ :after

Does not actually look for an :after pseudo-element that comes after an img within the a, even though it appears that way as both are rendered as children of the a element.

It can be rewritten into the following:

a[href^="http"] img ~ *:after

Notice the * selector, which is implied. Similarly to how you can omit * before any other simple selectors for it to be implied, omitting * from a pseudo-element also makes it implied to be there. See the spec for details.

Now, even though it appears *:after should still match a:after (since a would match *), it still doesn't work that way. If you remove the :after pseudo-element from the selector:

a[href^="http"] img ~ *

You'll notice that the meaning of the selector changes entirely:

Select any element
that appears as a following sibling of an img
that is a descendant of an a (whose href starts with "http").

Since the img is the last child of the a element in your HTML, there are no following siblings to match, and therefore no :after pseudo-elements can be generated.

In the case of a :before or :after pseudo-element, one might think of matching the pseudo-element's generating element relative to the pseudo-element's "sibling", but as the OP has correctly pointed out, there is no parent selector, so they're out of luck there, too.

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