简体   繁体   English

用Javascript设置访问链接的颜色

[英]Setting color of visited link with Javascript

divText += '<div class="single-article"> ';
divText += '<h2><a href="#" onClick="javascript:displayArticleDetail('+json._embedded.articles[i].articleId+', \''+escapedLinkTitle+'\'); setVisited();" \>'+json._embedded.articles[i].title+'</a>&nbsp;&nbsp;';

I need some assistance setting the color of a visited link. 我需要一些帮助来设置访问链接的颜色。 Having some trouble with this. 遇到一些麻烦。 I cannot use pure CSS :visited because this goes and changes the link color of every single link, not just the visited (clicked). 我不能使用纯CSS :visited因为这样做会改变每个链接的链接颜色,而不仅仅是访问(单击)。

How would I go about doing this with Javascript? 我将如何使用Javascript执行此操作?

You need to give more specificity to that anchor tag. 您需要为锚标记赋予更多的特异性。

Try giving it an id field and use a#id:visited when styling it 尝试给它一个id字段,并在样式设置时使用a#id:visited

You could also use class and select it with a.class:visited 您也可以使用class并使用a.class:visited选择它

Ideally this should solve your problem, 理想情况下,这应该可以解决您的问题,

a:visited {
    background-color: yellow;
}

but even if you want to handle it with javascript: 但即使您想使用javascript处理它,也可以:

you can use 您可以使用

document.getElementById("#elementID").style.color = "#ff0000";

or if you are using jQuery you can add a class to the element on click. 或者,如果您使用的是jQuery,则可以在单击时将一个类添加到元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM