开发者

swap classes on click

开发者 https://www.devze.com 2023-02-27 02:41 出处:网络
I have a list of 6 items that are in a global div (navigationagence) Right now I am able to add a class on click but right now they adds up which means that once my six items are clicked they all end

I have a list of 6 items that are in a global div (navigationagence) Right now I am able to add a class on click but right now they adds up which means that once my six items are clicked they all end up with the currentagence class.

I want to be able to remove the add a class to the click开发者_如何转开发ed item but remove it to the other, How can I achieve that? Here is the jquery code I am using right now.

$("#navigationagence ul li").click(function () { $(this).toggleClass("currentagence"); });


You can use the siblings() method to get the other list items and remove the class from them:

$("#navigationagence ul li").click(function() {
    $(this).addClass("currentagence").siblings().removeClass("currentagence");
});


Alternatively you can use:

$("#navigationagence ul li").click(function() {
    $(".currentagence").removeClass("currentagence");
    $(this).addClass("currentagence");
});
0

精彩评论

暂无评论...
验证码 换一张
取 消