[英]jquery .off() vs pointer-events: none
I have a button, upon clicking on which some logic is executed in jquery. 我有一个按钮,单击后会在jquery中执行一些逻辑。 After some other events I want the click on button to stop doing anything.
在发生其他一些事件之后,我希望单击按钮停止执行任何操作。 Which is better to use
$(".myBtn").off(click.mynamespace)
or $(".myBtn").css("pointer-events", none);
最好使用
$(".myBtn").off(click.mynamespace)
或$(".myBtn").css("pointer-events", none);
? ?
I think with pointer events none it literally disables any click, focus and hover action. 我认为在没有指针事件的情况下,它实际上会禁用任何单击,焦点和悬停操作。 And with jquery off it simply stops executing previously assinged
.click()
events. 如果关闭了jquery,它只会停止执行以前设置的.click
.click()
事件。
I suggest a third option: set the disabled
attribute on your button element. 我建议第三个选项:在按钮元素上设置
disabled
属性。 In addition to supressing mouse and click events, the default styling will change to appear grey, informing the user that the button can't be clicked. 除了抑制鼠标和单击事件外,默认样式还将更改为显示为灰色,告知用户无法单击按钮。 If you later want to re-enable the button, it's simple to remove the attribute.
如果以后要重新启用该按钮,则删除属性很简单。
$('button').prop('disabled', true); // disable buttons
$('button').prop('disabled', false); // enable buttons
With regards to your original two suggestions: 关于您最初的两个建议:
pointer-events
css property in this way would be a bad hack. pointer-events
css属性将是一件很糟糕的事情。 It's intended to let clicks "pass through" transparent elements to other controls in the background.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.