Set visited link color to whatever the color of un-visited link is (P.S. not the usual question)

前端 未结 10 1764
执念已碎
执念已碎 2021-02-03 16:59

I need to set the a:visited CSS to whatever color the normal a is set to.

What I want to be able to tell the browser is, for the visited links, use the same

10条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-03 17:39

    Presto:

    $(function(){
      var sheet = document.styleSheets[document.styleSheets.length-1];
      sheet.insertRule(
        'a:visited { color:'+$('a:link').css('color')+'; }',
        sheet.length
       );
    });
    

    I've tested and can confirm this works in Chrome. Keep in mind however, which sheet you're adding the rules to -- make sure its media attribute applies to the media that you care about. Additionally, if you have any rules that override the a coloring, this likely won't work properly -- so make sure your stylesheets are clear of that.

    I'm not so sure this is a very wise practice anyways. Personally, I always explicitly define my link colors for every site.

    PS:

    Apparently IE (don't know which versions) insists on their own syntax:

    sheet.addRule('a:visited', $('a:link').css('color'), -1);
    

提交回复
热议问题