事件.click()开启和关闭 [英] Event .click () on and off

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

问题描述

我通过这种方式进行了几次点击事件:

I have several click event made on this way:

$(function () {
    $('#button1').click(function () {
        //do samething...
    });

    $('#button2').click(function () {
        //do samething...
    });
    .....
});

在我的应用程序的不同部分,我想关闭这些点击,然后再将它们重新打开:

In different part of my application, I want to turn-off the clicks, and later turn-on them again:

我可以关闭调用此功能的点击:

I can turn off the click invoking this function:

function keysLock() {
    $('#button1').off('click');
};

现在我无法恢复点击功能.我尝试使用

Now I can't restore the click function. I tried with something like

$('#button1').on('click')

(以及许多类似的东西),但是它不起作用.

(and a lot of similar stuff) but it doesn't work.

推荐答案

只需根据您的评论添加替代解决方案,您就可以使用变量来启用/禁用事件,而不必删除处理程序. ,就像这样:

Just to add an alternative solution based on your comments, you can use a variable to enable/disable the events and you don't have to remove the handlers then, something like that:

$(function () {
  var someSpecificCondition = false;

  function doThisStuffOnClick () {
    if (someSpecificCondition) {
      return;
    }
    // do something
  }

  $('#button1, #button2').click(doThisStuffOnClick);    
});

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

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