admin管理员组文章数量:1435859
I have this menu
<ul id="menu" class="clearfix">
<li>
<a href="">Product 1</a>
</li>
<li>
<a href="">Product 2</a>
</li>
<li>
<a href="">Product 3</a>
</li>
<li class="last">
<a href="">Product 4</a>
</li>
</ul>
I want to make this effect. when you enter the page the text color of the menu items are white. If you click on one item it bees active (keeping the white color text) and all the other items change it's color to gray, also when you hover over one the affected items are all the others.
I have tried with the .addClass but I have only managed to add the active class to the current item, but not change the others that aren´t active after the first click.
Anyone know the best jquery approach to this?
I have this menu
<ul id="menu" class="clearfix">
<li>
<a href="">Product 1</a>
</li>
<li>
<a href="">Product 2</a>
</li>
<li>
<a href="">Product 3</a>
</li>
<li class="last">
<a href="">Product 4</a>
</li>
</ul>
I want to make this effect. when you enter the page the text color of the menu items are white. If you click on one item it bees active (keeping the white color text) and all the other items change it's color to gray, also when you hover over one the affected items are all the others.
I have tried with the .addClass but I have only managed to add the active class to the current item, but not change the others that aren´t active after the first click.
Anyone know the best jquery approach to this?
Share Improve this question asked Nov 14, 2012 at 18:24 codekcodek 3435 gold badges20 silver badges49 bronze badges 2-
when you hover over one the affected items are all the others.
What does this mean? – Justin Morgan Commented Nov 14, 2012 at 18:38 - the other people got it ;) haha sorry, as you may have guessed english is not my main language – codek Commented Nov 14, 2012 at 18:40
4 Answers
Reset to default 2$("#menu li").hover(function() {
$(this).removeClass('grey').siblings().addClass('grey');
}, function() {
$(this).addClass('grey').siblings('.active').removeClass('grey');
//
}).on('click', function() {
$(this).removeClass('grey').addClass('active').siblings().addClass('grey').removeClass('active')
});
http://jsfiddle/y7Cn5/
Perhaps something like this:
Updated, with hover functionality
jsFiddle
$("#menu > li").on("click", function(e) {
e.stopPropagation();
$(".active").removeClass("active");
$(this).removeClass("non-active").addClass("active").siblings().addClass("non-active");
})
.hover(function(e) {
$(".non-hover").removeClass("non-hover");
$(this).addClass("hover").siblings().addClass("non-hover");
}, function(e) {
$(".hover, .non-hover").removeClass("hover non-hover");
})
and if this doesn't answer the question, then the question is not understood and needs some rewording, this is everything asked about in the question
Here you go (siblings) selects all OTHER items in the same node
$("#menu li").click(function(){
$(this).addClass("active").css("color","white")
$(this).siblings().css("color","gray")
})
Try
$("#menu > li").hover( function( ) {
$(this).removeClass("hovered").siblings().addClass("hovered");
}, function( ) {
$(this).addClass("hovered");
});
$("#menu > li").click( function( ) {
$(this).addClass("active").siblings().removeClass("active");
});
本文标签: javascriptAdd active class and change css of other itemsStack Overflow
版权声明:本文标题:javascript - Add active class and change css of other items - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745613706a2666269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论