当您定义所有 4 种状态时,您应该按以下顺序定义它们:
- 链接
- 已访问
- 悬停
- 活动
这解决了大约一半的问题。
a:link { color: blue; }
a:visited { color: blue; }
a:hover { color: red; }
a:active { color: green; }
另一半,让链接保持颜色,直到你点击其他东西,更难。之前单击的任何内容都没有内置状态,所以让我们制作一个。
首先,我们需要 jQuery,所以让我们导入它(通过 Google)。这在您的 HTML 的头部:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">
</script>
在 jfiddle 中,您可以通过从左侧下拉列表中选择 jQuery 2.1.4 来添加它。
如果链接是最后点击的链接,我们可以通过提供此 JavaScript 来为链接添加一个类:
$(function(){
$('a').click(function(){
$('a.lastclicked').removeClass('lastclicked'); //remove class from previous link
$(this).addClass('lastclicked'); //add class to newly clicked link
});
});
最后,让我们调整 CSS 来进行着色:
a:link { color: blue; }
a:visited { color: blue; }
a:hover { color: red; }
a:active { color: green; }
a.lastclicked { color: green; }
如果您也希望将悬停颜色应用于最后点击的链接,则可以添加以下行:
a.lastclicked:hover { color: red; }
您可以在 in a Fiddle here 中看到所有这些。