单击交换类 [英] swap classes on click

查看:71
本文介绍了单击交换类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全局div中的6个项目的列表(navigationagence) 现在,我可以在单击上添加一个类,但是现在它们可以相加,这意味着一旦单击我的六个项目,它们都将以currentagence类结束.

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.

我希望能够删除在单击的项目中添加类但将其删除到其他项目的方法,我该如何实现?这是我现在正在使用的jquery代码.

I want to be able to remove the add a class to the clicked item but remove it to the other, How can I achieve that? Here is the jquery code I am using right now.

推荐答案

您可以使用 siblings()获取其他列表项并从中删除类的方法:

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");
});

这篇关于单击交换类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆