当其他链接被点击时,我该如何将超链接颜色更改回原始颜色?这些超链接指向同一页。
请查看此 DEMO
从上面的例子中可以看到,当点击苹果后再点击葡萄/香蕉..两者变成了相同的颜色(因为visited)。 如何使它在任何链接被单击时只有一种颜色(绿色)
jQuery
实现这一点。
$('body a:link').click(function()
{
$('body a').removeClass('active');
$(this).addClass('active');
});
a:link {
color: blue;
}
/* visited link */
a.active {
color: green;
}
/* mouse over link */
a:hover {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="fruit" href="#apple">apple</a></span>
<a class="fruit" href="#grape">grape</a></span>
<a class="fruit" href="#banana">banana</a></span>
<div style="height:500px"></div>
<a name="apple"> apple here</a>
<a name="grape"> grape here</a>
<a name="banana"> banana here</a>
:active
和:focus
选择器为我们提供了有限的解决方案。 - om_jaipur这可以解决你问题的一半。
a:link { color: blue; }
a:visited { color: blue; }
a:hover { color: red; }
a:active { color: green; }
首先,我们需要使用jQuery,因此让我们通过谷歌导入它。这将放在你的HTML头部:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">
</script>
如果它们是最后点击的链接,则为链接添加一个类:
$(function(){
$('a').click(function(){
$('a.lastclicked').removeClass('lastclicked'); //remove class from previous link
$(this).addClass('lastclicked'); //add class to newly clicked link
});
});
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; }
请尝试以下代码(复制并粘贴即可):
Test.html:-
<html>
<link rel="stylesheet" type="text/css" href="style.css">
<a class="fruit" href="#">apple</a></span>
<a class="fruit" href="#">grape</a></span>
<a class="fruit" href="#">banana</a></span>
<div style="height:500px"></div>
<a name="apple"> apple here</a>
<a name="grape"> grape here</a>
<a name="banana"> banana here</a>
</html>
style.css:-
a:link{
color:blue;
}
a:visited{
color:purple;
}
a:hover{
color:orange;
}
a:focus{
color:green;
}
a:active{
color:red;
}
a:active{
color:yellow;
}
a:active{color:green}
并在访问后保留蓝色。 - Fahad Khan