简体   繁体   中英

Disable bound click event in jquery

I try to disable the binded click event in jquery, but I can found only to unbind or off the event click. If I do unbind() I can't get back those binded functions back.

For eg: I have a three div binded various function with click event. In some case I dont want particular div click event. So I try to disable that event only can't try to unbind() or off() the event. If I unbind() the event from div then again I need to bind those functions back to that div. See fiddle

Is there a way to disable the event and enable back those events without unbinding it.

Thanks in advance.

You can stop event like this:

function handler(e){
    if(yourcondition)
        e.preventDefault();
    else {

    }
}

You can give a class name to those div's. And call the click event though this class name like

$(".class_name").click(function(){ });

and when you want to disable the click event then use the id selector to remove the class

$("#div_id").removeClass("class_name");

and when you want to add the click event then then use the id selector to add the class

$("#div_id").addClass("class_name");

in this example

http://jsfiddle.net/prollygeek/WuNGJ/2/

you can remove class pointer from all elements so that no element will respond to any bound action , once you trun back the class , it will respond normally , class binding and unbining can be done by clicking div 1.

$(".element").on('click',function(){
  if($(this).hasClass("triggered"))
     {

$(".element").addClass('pointer');
$(this).removeClass("triggered");
}
else if($(this).hasClass("pointer"))
     {

$(".pointer").removeClass("pointer");
$(this).addClass("triggered");
}
//do whatever
})     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM