[英]jquery toggle/untoggle add/remove class
I have this piece of code here: 我在这里有这段代码:
$('.plus-area-current').click(function () {
$(".current-communities").slideToggle(function () {
$(".plus-area-current i").removeClass("fa-plus");
$(".plus-area-current i").addClass("fa-minus");
}, function () {
$(".plus-area-current i").removeClass("fa-minus");
$(".plus-area-current i").addClass("fa-plus");
});
});
When the user clicks plus-area-current, current-communities gets untoggled, what I am trying to is add / remove the fa-plus and fa-minus classes from an element pending if the element current-communities is toggled or not. 当用户单击plus-area-current时,current-communities无法切换,我要尝试的是从元素中添加/删除fa-plus和fa-minus类,而无需切换元素current-communities。 This code above does not work, the element toggles but the class does not change, if I do this:
上面的这段代码不起作用,如果我这样做,元素会切换,但类不会改变:
$('.plus-area-current').click(function () {
$(".current-communities").slideToggle();
$(".plus-area-current i").removeClass("fa-plus");
$(".plus-area-current i").addClass("fa-minus");
});
It works, but when I click to toggle it back up, its does not change back to the plus. 它可以工作,但是当我单击以将其备份时,它不会变回加号。
Use toggleClass()
instead: 使用
toggleClass()
代替:
$('.plus-area-current').click(function () {
$(".current-communities").slideToggle();
$(".plus-area-current i").toggleClass("fa-plus fa-minus");
});
If you'd prefer the class change not to happen until the slideToggle()
animation has completed, place that line within the callback, like this: 如果您希望在
slideToggle()
动画完成之前不进行类更改,请将该行放在回调中,如下所示:
$('.plus-area-current').click(function () {
$(".current-communities").slideToggle(function() {
$(".plus-area-current i").toggleClass("fa-plus fa-minus");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.