简体   繁体   English

如何更改活动链接的颜色?

[英]How to change the color of an active link?

I want to change to color red of selected link.我想将所选链接的颜色更改为红色。

For example I clicked the link text dog, the color will change to red because it's currently selected.例如,我单击了链接文本狗,颜色将更改为红色,因为它当前已被选中。 If I clicked the link text cat the color will change to red and link text dog will go back to default link color.如果我单击链接文本 cat 颜色将变为红色,链接文本 dog 将返回默认链接颜色。

HTML HTML

<ul>
   <li> <a href="#"> dog </a> </li> 
   <li> <a href="#"> cat </a> </li>

</ul>

CSS CSS

??

You can use :focus pseudo-class.您可以使用:focus伪类。

The :focus CSS pseudo-class is applied when an element has received focus, either from the user selecting it with the use of a keyboard or by activating with the mouse (eg a form input). :focus CSS 伪类在元素获得焦点时应用,无论是用户使用键盘选择它还是使用鼠标激活(例如表单输入)。

 a:focus { color: red; }
 <ul> <li> <a href="#"> dog </a> </li> <li> <a href="#"> cat </a> </li> </ul>

But if you want link to stay red when you click somewhere else on page then you can use js但是,如果您希望在单击页面上的其他位置时链接保持红色,那么您可以使用 js

 $('li').click(function() { $(this).siblings().find('a').removeClass('focus'); $(this).find('a').addClass('focus'); });
 .focus { color: red; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> <a href="#"> dog </a> </li> <li> <a href="#"> cat </a> </li> </ul>

 a:focus { color: red; }
 <ul> <li> <a href="#"> dog </a> </li> <li> <a href="#"> cat </a> </li> </ul>

You can use the :visited selector to style all the visited document links.您可以使用 :visited 选择器来设置所有访问过的文档链接的样式。 Check http://www.w3schools.com/cssref/sel_visited.asp检查http://www.w3schools.com/cssref/sel_visited.asp

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

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