在jQuery中打开所有事件.off()和.on() [英] Turning all events .off() and .on() in jQuery

查看:73
本文介绍了在jQuery中打开所有事件.off()和.on()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用.off().on()在jQuery中打开和关闭事件处理程序.

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

我可以使用以下方法完全关闭所有附加的事件处理程序:关闭:

I can turn the all attached event handlers off perfectly by using:

$('.eventone').off();

或作为多重选择器:

$('.eventone, .eventtwo, .eventthree').off();

但是,我没有让所有(甚至任何一个)事件处理程序重新打开打开.我正在使用这个:

However, I'm not getting all (or even any) event handlers to turn back on. I'm using this:

$('.eventone, .eventtwo, .eventthree').on();

我在做错什么吗?

实际上,我是否必须声明与相关联的每个事件才能将其重新打开?

Do I actually have to declare every event that's associated with the class in order to turn it back on?

推荐答案

是的,.off基本上删除了事件,因此您将重新声明它们.您最好拥有一个全局变量,以便事件处理程序可以检查它是否设置为off或on,以及off只是返回.只需将变量更改为off或on,true或false或任何适合您的

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;

这篇关于在jQuery中打开所有事件.off()和.on()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆