在jQuery中切换2个类的最简单方法 [英] Easiest way to toggle 2 classes in jQuery
问题描述
如果我有.A类和.B类,并且想要在单击按钮之间切换,那么在jQuery中有什么好的解决方案?我仍然不明白toggleClass()
的工作原理.
If I have class .A and class .B and want to switch in between on button click, what's a nice solution for that in jQuery? I still don't understand how toggleClass()
works.
是否存在将其放入onclick=""
事件的嵌入式解决方案?
Is there an inline solution to put it in onclick=""
event?
推荐答案
如果您的元素从一开始就公开类A
,则可以编写:
If your element exposes class A
from the start, you can write:
$(element).toggleClass("A B");
这将删除类A
并添加类B
.如果再次执行此操作,它将删除类B
并恢复类A
.
This will remove class A
and add class B
. If you do that again, it will remove class B
and reinstate class A
.
如果要匹配公开这两个类的元素,则可以使用多个
If you want to match the elements that expose either class, you can use a multiple class selector and write:
$(".A, .B").toggleClass("A B");
这篇关于在jQuery中切换2个类的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!