admin管理员组文章数量:1336632
I have two class empty and colored. Once I clicked colored class then remove colored class and add empty class. Again I click on it it should be add colored class and remoce empty class. But it is't working.
var color_click = false;
var select_color = "";
$( ".colored").on('click',function(e){
if(color_click != true){
color_click = true;
select_color = $(this).css('background-color');
$(this).removeClass("colored");
$(this).addClass( "empty");
$(this).css('background-color','')
}
});
$( ".empty").click(function(){
if(color_click == true){
color_click = false;
$(this).css('background-color',select_color);
$(this).addClass("colored");
$(this).removeClass( "empty");
}
});
I have two class empty and colored. Once I clicked colored class then remove colored class and add empty class. Again I click on it it should be add colored class and remoce empty class. But it is't working.
var color_click = false;
var select_color = "";
$( ".colored").on('click',function(e){
if(color_click != true){
color_click = true;
select_color = $(this).css('background-color');
$(this).removeClass("colored");
$(this).addClass( "empty");
$(this).css('background-color','')
}
});
$( ".empty").click(function(){
if(color_click == true){
color_click = false;
$(this).css('background-color',select_color);
$(this).addClass("colored");
$(this).removeClass( "empty");
}
});
Share
Improve this question
asked Jun 23, 2013 at 6:32
Mangala EdirisingheMangala Edirisinghe
1,1112 gold badges16 silver badges32 bronze badges
1
-
you probably need to add the event handler again, after
addClass
– tay10r Commented Jun 23, 2013 at 6:35
3 Answers
Reset to default 5Yes. That is because you bind the event to that particular class. You can use event delegation to resolve the issue using on(). When your event binding happens there is no element with the class .empty
and the binding has no effect. Instead of using the document head(as used in my example) use a container that exists in DOM all the time and holds this element. So with event delegation you are actually binding the event to a container/document head for delegation on the elements that are present in DOM now as well as for the future.
Apart from this i have made some changes to remove some ambiguous check and use chaining.
$(document).on('click', ".colored", function(e){
if(!color_click){ // You dont need this check if your variable is modified only in these 2 events
color_click = true;
select_color = $(this).css('background-color');
$(this).removeClass("colored").addClass( "empty").css('background-color','');
}
});
$( document).on('click', ".empty", function(){
if(color_click){// You dont need this check if your variable is modified only in these 2 events
color_click = false;
$(this).addClass("colored").removeClass("empty").css('background-color',select_color);
}
});
You need to rebind the click handlers for the classes
Wrap it up in a function (eg bindClicks), and then call bindClicks() when you add new classes.
put the $(".empty").click
code immediately after you assign the class to the element. On DOMReady this click handler does nothing, since there is no element with that class and when you change the class DOM Ready is not being called again. and vice versa.
var color_click = false;
var select_color = "";
bindColor(); bindEmpty();
function bindEmpty() {
$(".empty").click(function () {
if (color_click == true) {
color_click = false;
$(this).css('background-color', select_color);
$(this).addClass("colored");
$(this).removeClass("empty");
bindColor();
}
});
}
function bindColor() {
$(".colored").on('click', function (e) {
if (color_click != true) {
color_click = true;
select_color = $(this).css('background-color');
$(this).removeClass("colored");
$(this).addClass("empty");
$(this).css('background-color', '')
bindEmpty()
}
});
}
本文标签: javascriptremoveClass() after addClass() not workingStack Overflow
版权声明:本文标题:javascript - .removeClass() after .addClass() not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742413725a2470300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论