如果其他人添加删除类中的错误 [英] bug in add remove class with if else
本文介绍了如果其他人添加删除类中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$('。lable_item')。click(function(){
if(!$(this).hasClass('label_act')){
$(this).addClass('label_act');
} else {
$(this).removeClass( 'label_act');
}
});
这是我的HTML内容
< div class =layout>
< label class =lable_item>< input type =checkbox/> label< / label>
< label class =lable_item label_act>< input type =checkbox/> label< / label>
< label class =lable_item>< input type =checkbox/> label< / label>
< / div>
我想在标签上添加/删除类。如果标签有label_act,那么它将被删除,否则将添加类label_act。
解决方案
为了不让它运行2次单击时必须使用输入
而不是标签
$('。layout input')。click(function(){
$(this).parent()。toggleClass('label_act')
});
Here is my Javascript code
$('.lable_item').click(function () {
if (!$(this).hasClass('label_act')) {
$(this).addClass('label_act');
} else {
$(this).removeClass('label_act');
}
});
Here is my HTML Content
<div class="layout">
<label class="lable_item"><input type="checkbox" />label</label>
<label class="lable_item label_act"><input type="checkbox" />label</label>
<label class="lable_item"><input type="checkbox" />label</label>
</div>
I want to add/remove class on lable tag. If label has "label_act", then it will be remove otherwise add class "label_act"
解决方案
In order for it not to run 2 times when you click you have to use the input
instead of label
$('.layout input').click(function () {
$(this).parent().toggleClass('label_act')
});
这篇关于如果其他人添加删除类中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文