我有一个顶部导航链接,它驱动左侧导航中的链接。顶部导航包含一堆主题。单击时每个主题链接都会使用该主题的相关链接填充左侧导航栏。因此,当单击顶部导航栏上的主题时,我希望它的颜色更改为红色,而其余的颜色为蓝色,但是一旦单击另一个标题,我希望先前单击的链接将其颜色恢复为蓝色,并且新点击一个获得红色。我们如何在 jquery 中做到这一点?添加类和删除类以及使用 css 不起作用..请帮助..
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MY C# Notes</title>
<style type="text/css">
body,
html {margin:0; padding:0;color:#000;background:#a7a09a;}
#wrap {width:1050px;margin:0 auto;background:#99c;}
#header {padding:5px 10px;background:#ddd;}
h1 {margin:0;}
#nav {padding:5px 10px;background:#c99;}
#nav ul {margin:0;padding:0;list-style:none;}
#nav li {display:inline;margin:0;padding-right:15px;}
#main { float:right;width:780px;padding:10px;background:#9c9;min-height:600px;}
h2 {margin:0 0 1em;}
#sidebar {float:left;width:230px;padding:10px;background:#99c;min-height:600px;}
#footer {clear:both;padding:5px 10px;background:#cc9;}
#footer p { margin:0; }
* html #footer {height:1px;}
.high{color:red}
a.loader { color: blue; }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
$('a.loader').on('click', function(e) {
e.preventDefault();
$('#sidebar').load($(this).attr('href'));
$("a.loader").removeClass("high");
$(this).addClass("high");});
});
</script>
<div id="wrap">
<div id="header"><h1>My C# Notes and Exercises</h1></div>
<div id="nav">
<ul>
<li><a class="loader" href="topic1.html">Topic1</a></li>
<li><a class="loader" href="topic2.html">Topic2</a></li>
<li><a class="loader" href="topic3.html">Topic3</a></li>
<li><a class="loader" href="topic4.html">Topic4</a></li>
<li><a href="/">LocalBrowser</a></li>
</ul>
</div>
<div id="main">
</div>
<div id="sidebar"></div>
<div id="footer">
<p>C# Online Tutorials</p>
</div>
</div>
</body>
</html>
最佳答案
您的 css 类 .loader
优先于 .high
;您可以通过以下方式解决此问题:
-将 !important
添加到 .high
的声明中:.high {color: red !important;}
-交换你的 css 声明的顺序:a.loader { color: blue;} a.high { color: red; }
-或在添加 .high
时删除 .loader
:
$(document).ready(function() {
$('a.loader').on('click', function(e) {
e.preventDefault();
$('#sidebar').load($(this).attr('href'));
$("a.high").not(this).removeClass("high").addClass('loader');
$(this).removeClass('loader').addClass("high");
});
});
关于javascript - 为点击的链接赋予不同的颜色,并在点击其他链接时恢复其颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12030919/