css - 为什么这不是 :visited css style work?

标签 css anchor visited

是否有任何原因导致这在 Internet Explorer 或 Chrome 上不起作用:

<html>
    <head>
        <style>
            A {font-weight: bold; color:black;}
            A:visited {font-weight: normal; color: black; }
            .Empty {font-weight: bold; color: black; }
        </style>
    </head>

    <body>
        <a href="http://mysite">click me</a>
    </body>
</html>

我点击的链接永远不会正常,只是保持粗体。在一些其他浏览器上它可以工作。

改变大小写并没有影响它。将 a 更改为 a:link 不会影响它。更改颜色有效,但字体粗细无效。

一种解决方法是更改​​可访问性以忽略 Web 颜色。我无权访问源代码,所以我不得不这样做。

最佳答案

实际上,这与区分大小写无关。这是一项安全功能。 :visited 伪类的功能已在许多现代浏览器(Fx4、IE9、Chrome)中受到限制,以防止 CSS 漏洞利用:了解它 here .

如今,这些浏览器中的 getComputedStyle() 通常会为访问过的链接返回值,就好像它们没有被访问过一样。但是,我可以简单地想象如何绕过它:对访问过的链接使用 font-weight,元素的宽度会发生变化,因此浏览器将允许为 更改 font-weight: visited 链接实际上并不能修复安全漏洞。

可以看到有some specific things浏览器会采取措施防止这种情况发生:

  • The window.getComputedStyle method, and similar functions such as element.querySelector, will always return values indicating that a user has never visited any of the links on a page.
  • If you use a sibling selector such as :visited + span, the adjacent element (span in this example) will be styled as if the link were unvisited.
  • In rare scenarios, if you're using nested link elements and the element being matched is different from the link whose presence in history is being tested, the element will be rendered as if the link were unvisited, as well.

因此,此问题没有解决方法。

关于css - 为什么这不是 :visited css style work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8331688/

相关文章:

html - 将 <div> 与父级 <div> 的右下角对齐?

jquery - Zurb Foundation 顶部栏菜单无法在移动显示屏上运行

jquery - 从当前 url 添加/删除 anchor 名称而不刷新

html - 如何为访问过的链接添加复选标记(链接展开一段,复选表示 "done with reading it")?

html -::before 和::after 伪元素问题

CSS 网格不符合

C# 无法在 VisualStudio 上对 iOS 应用程序的按钮使用约束

php - 使用 php 动态创建的页面滚动到 anchor 标记

CSS伪a :link overwriting text-decoration for a:visited

css - 我可以使用 CSS :visited pseudo class on 'wildcard' links?