问题描述
我知道这非常简单,但是我已经编写和重写了这个函数一个小时,但没有成功:
i know this is incredibly easy but I have been writing and rewriting this function for an hour with no success:
在单击时,我要向单击的链接的<li>
中添加活动",并从导航栏中的所有其他<li>
中删除活动".
On click I want to add "active" to the <li>
of the clicked link and remove "active" from all the other <li>
's in the navbar.
HTML:
<nav>
<ul>
<li><a href="http://0327ea8.netsolhost.com/#home" class="home">Home</a></li>
<li><a href="http://0327ea8.netsolhost.com/blog/" class="blog">Blog</a></li>
<li><a href="http://0327ea8.netsolhost.com/#catalog" class="catalog">Catalog</a></li>
<li><a href="http://0327ea8.netsolhost.com/#authors" class="authors">Authors</a></li>
<li><a href="http://0327ea8.netsolhost.com/#store" class="store">Store</a></li>
</ul>
</nav>
jQuery:
// NAVBAR ADD ACTIVE
$("nav ul li a").click(function(){
$("nav ul li").removeClass('active');
$(this).stop().animate();
$(this).parent().addClass('active');
});
这可以很好地添加类,但是我无法删除它.
This is adding the classes just fine but I can't get it to remove.
我也尝试过将其分离为独立的函数,
I've also tried separating it into to separate functions with no luck.
我在其中具有.stop().animate(),因为在链接的悬停状态上具有.animate().
I have the .stop().animate() in there because I have a .animate() on the hover state of the links.
谢谢!
这是悬停的jQuery,也是我拥有.stop().animate()
this is the jQuery for the hover and the reason I have .stop().animate()
$("nav ul li a, aside ul li.categories a, div.posts div.foot a").hover(function(){
$(this).animate({'backgroundColor' : '#edf5e9'}, 200);
},function() {
$(this).animate({'backgroundColor' : '#ccd4c8'}, 600);
});
我不确定.stop().animate()为什么出错.如果我没有.animate(),则活动类将不会停留,如果存在,活动类将不会停留.
I'm not sure why the .stop().animate() is wrong. If I don't have .animate() the active class won't stick, if I do, the active class does.
我仍然无法通过:-/
推荐答案
尝试一下:
$(this).parent().siblings().removeClass('active');
这篇关于jQuery从.parent()中添加/删除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!