jQuery 禁用,启用('点击')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13276291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable, Enable on('click')
提问by Nomistake
How can i disable and enable a on click event.
如何禁用和启用点击事件。
I tried with:
我试过:
$('#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...
但这也不起作用......
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?
我如何实现事件以停止传播?
How can i do this?
我怎样才能做到这一点?
回答by Selvakumar Arumugam
You need to pass the handler function as the argument whenever you bind
(or rebind
).
无论何时bind
(或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
回答by Kaleem Nalband
first disable the element using prop()
method of jQuery, i.e.
首先使用prop()
jQuery的方法禁用元素,即
$("selector").prop("disabled",true);
and when you want to enable click the just do it by using
当您想启用时,单击只需使用
$("selector").prop("disabled",false);