简体   繁体   English

jQuery .off()与指针事件:无

[英]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: 关于您最初的两个建议:

  • If the button will never be re-enabled, consider removing it entirely, don't just remove its click event listener. 如果该按钮永远不会被重新启用,请考虑将其完全删除,而不仅仅是删除其click事件监听器。 Conversely, don't remove the event listener if the button is likely to be re-enabled with the same action. 相反,如果可能通过相同的操作重新启用了按钮,则不要删除事件侦听器。
  • Using the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM