styling visited links belonging to a class

谁都会走 提交于 2020-01-06 15:42:12

问题


I cannot find a way to style a:visited but only those belonging to .extern.

a:visited.extern doesn't work and neither does a.extern:visited (I' using Mozilla Firefox 43.0.1 for Linux x86_64)

The reason is I have a small icon I'd like to add to .extern links, and I want to change its url() when the link is visited.

<style>
    a.extern {
        padding-right:1.3em;
        background-repeat: no-repeat;
        background-attachment: scroll;
        background-position: right center;
        background-clip: border-box;
        background-origin: padding-box;
        background-size: 0.7em 0.7em;
        background-image: url("img/link.png");
    }

    a:visited:extern {
        background-image:url("img/link-visited.png");
    }

    a.extern:visited {
        background-image:url("img/link-visited.png");
    }
</style>

In the end all visited links of this type should be affected

<a class="extern" href="http://etc.etc.etc">link</a>

回答1:


Basically what you want to do is restricted by browsers, particularly Firefox.

Per MDN - Privacy and the :visited selector

You will still be able to visually style visited links, but there are now limits on what styles you can use. Only the following properties can be applied to visited links:

  • color
  • background-color
  • border-color (and its sub-properties)
  • outline-color
  • The color parts of the fill and stroke properties



回答2:


First, I assume the name of your class is extern, and not .extern, so your a tag will look something like this.

<a class="extern" href="http://etc.etc.etc">link</a>

To style only the visited links which have the extern class you should use:

a.extern:visited {
    background-image:url("img/link-visited.png");
}

Notice how the :visited selector is added at the end: a.extern:visited.




回答3:


Change

<a class=".extern" href="http://etc.etc.etc">link</a>

to

<a class="extern" href="http://etc.etc.etc">link</a>

and

<style>
    a.extern {
        padding-right:1.3em;
        background-repeat: no-repeat;
        background-attachment: scroll;
        background-position: right center;
        background-clip: border-box;
        background-origin: padding-box;
        background-size: 0.7em 0.7em;
        background-image: url("img/link.png");
    }

    a:visited:extern {
        background-image:url("img/link-visited.png");
    }

    a.extern:visited {
        background-image:url("img/link-visited.png");
    }
</style>

block with code below do not needed

a:visited:extern {
    background-image:url("img/link-visited.png");
}


来源:https://stackoverflow.com/questions/34827812/styling-visited-links-belonging-to-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!