禁用,启用on('click') [英] Disable, Enable on('click')

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

问题描述

如何禁用和启用点击事件.

How can i disable and enable a on click event.

我尝试过:

$('#web').on('click', function web_function(event){
    event.stopPropagation();
    // execute a bunch of action to preform
});
$('#web').off('click'); // click is succesfully removed
$('#web').on('click'); // doesnt work, i need to redefine the actions to perform

我也尝试通过以下方式禁用:

Also I tried disabling with:

$('#web').unbind('click'); // click is succesfully removed
$('#web').bind('click'); // nok

但这也不起作用...

But this also doesnt work...

因此,我想禁用/启用click事件,而无需重新定义要执行的操作.某种切换...(单击开/关)

So, i would like to disable/enable the click event without the need to redefine the actions to perform. Some sort of toggle... (click on/off)

该如何实现停止传播的事件?

And how do i implement the event to stop propagation?

我该怎么做?

推荐答案

无论何时bind(或rebind),都需要将处理函数作为参数传递.

You need to pass the handler function as the argument whenever you bind (or rebind).

在您的情况下,您可以命名该函数,以便在您再次重新绑定时可以通过该函数..见下文,

In your case, you can name the function which you can use to pass it any time you re-bind again.. see below,

var myFunc = function(event){
     event.stopPropagation();
     // execute a bunch of action to preform
}

$('#web').on('click', myFunc); //bind myFunc
$('#web').off('click'); // click is succesfully removed
$('#web').on('click', myFunc); //rebind again

这篇关于禁用,启用on('click')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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