Turning all events .off() and .on() in jQuery

前端 未结 1 528
无人及你
无人及你 2021-01-16 08:43

I\'m looking at using .off() and .on() to turn event handlers off and on in jQuery.

I can turn the all attached event hand

相关标签:
1条回答
  • 2021-01-16 09:40

    Yes, .off basically removes the events so you will have redeclare them. you are better off having a global variable so your event handlers can check to see if it set to off or on and if off just return. And just change the variable to off or on, true or false or whatever suits you

    var eventsOn = false;
    function myFunc() {
       if(!eventsOn) return;
    
       //Code to run here.
    }
    
    //Somewhere else set your events and turn eventsOn to true
    jQuery("#mydiv").on("click",myFunc);
    eventsOn = true;
    
    0 讨论(0)
提交回复
热议问题