css - 我可以定位 :before or :after pseudo-element with a sibling combinator?

标签 css css-selectors pseudo-element

是否有此 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>

这个想法是在匹配的 anchor 标签上有一个伪元素。但我不希望它应用于包装图像的 anchor 标签。而且由于我无法使用 a < img 之类的东西来定位 anchor ,我想也许我可以通过找到一张它是 sibling 的图像来定位 :after 伪元素。

如有任何见解,我们将不胜感激。

最佳答案

你不能定位 :after 因为它的内容不在 DOM 中呈现并且它不操纵它 - 为此,DOM 必须重新呈现并且 CSS 不能像这样操纵它。

查看规范以了解详细信息: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).

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

关于css - 我可以定位 :before or :after pseudo-element with a sibling combinator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7735267/

相关文章:

html - 水平对齐按钮

html - 在每个 tr 标记行后插入换行符

html - TD 内容上的 CSS 选择器

html - 为什么嵌套在 css 类选择器中不能按预期工作

javascript - 如何增加十六进制数

css - 使伪元素的高度与其附加的元素相同

css - openwebkitsharp 读/写保护内存

python - 如何使用 Selenium 单击此项目?

css - 为什么我不能在伪元素中使用伪元素?

php - 在 PHP 文件中包含 CSS 文件包含在 PHP 文件中