我们可以在 jquery 中使用简单的 onclick 颜色更改行为,因为其他一切都已经正常工作了。所以你可以做的就是改变点击的颜色,然后把它从之前的颜色中删除。
对于以下 HTML:
<nav>
<li id="1"><a href="#" >One</a></li>
<li id="2"><a href="#" >Two</a></li>
<li id="3"><a href="#" >Three</a></li>
<li id="4"><a href="#" >Four</a></li>
</nav>
如果每个导航项的 onclick 颜色保持相同,则
jQuery(document).ready(function($) {
$("nav li").each(function() {
$(this).click(function() {
$(this).prev().find("a").css("color", "red");
$(this).next().find("a").css("color", "red");
$(this).find("a").css("color", "blue");
});
});
});
如果每种颜色不同。
jQuery(document).ready(function($) {
$("#1").click(function() {
$(this).prev().find("a").css("color", "red");
$(this).next().find("a").css("color", "red");
$(this).find("a").css("color", "blue");
});
})
除了.css,你还可以添加一个类似
的类
jQuery(document).ready(function($) {
$("nav li").each(function() {
$(this).click(function() {
$(this).prev().find("a").removeClass("active");
$(this).next().find("a").removeClass("active");
$(this).find("a").addClass( "active" );
});
});
});
https://jsfiddle.net/yuhazt0p/1/