禁用/启用jQuery onClick函数 [英] Disabling/Enabling Jquery onClick Functions

查看:623
本文介绍了禁用/启用jQuery onClick函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不解除绑定的情况下禁用"或暂停" jquery .click函数?我想让函数绑定,但不希望它触发.然后,稍后,我想重新启用它.

Is there any way to 'disable' or 'suspend' a jquery .click function, without unbinding? I want to leave the function bound, but don't want it to fire. Then, at a later time, I would like to re-enable it.

在这种情况下,仅禁用绑定到的控件是不够的.

Just disabling the control being bound to will not suffice in this case.

推荐答案

您可以在更高的变量范围内设置标志,并且仅在标志打开时才执行处理程序.这是一个简单的示例:

You could set a flag in a higher variable scope and only execute the handler if the flag is on. Here's a simple example:

$(document).ready(function () {
    var execHandler = false;
    $('input').click(function (e) {
        if (execHandler) {
            //do stuff
        }
        e.preventDefault();
        return false;
    });
    $('button#toggleHandler').click(function (e) {
        execHandler = !execHandler;
        e.preventDefault();
        return false;
    })
});

这是一个类似的解决方案,用于:暂时禁止jQuery事件处理

It's a similiar solution to: Suppress jQuery event handling temporarily

这篇关于禁用/启用jQuery onClick函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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