简体   繁体   English

如何使用jQuery禁用/启用DOM节点及其所有后代元素的鼠标/敲击事件?

[英]How do I disable/enable mouse/tap events for a DOM node and all its descendant elements with jQuery?

Say I have a DOM like this: 说我有一个这样的DOM:

 <div id="root"> <div> <button>Button</button> </div> <section> <a href="#">Link</a> <span onclick="alert(1);">Text</span> </section> </div> 

I want to make all the elements inside the #root node unable to be clicked/tapped. 我想使#root节点内的所有元素都无法被单击/点击。 What's the best way to do this? 最好的方法是什么?

Thanks, 谢谢,

You can use CSS pointer events 您可以使用CSS指针事件

 #root { pointer-events: none; } 
 <div id="root"> <div> <button>Button</button> </div> <section> <a href="#">Link</a> <span onclick="alert(1);">Text</span> </section> </div> 

Add a function at click of button Function atbottonclick(){$(this).css('pointer-events','none')} But you need to prevent keyboard events also for totally disable them. 单击按钮添加功能Function atbottonclick(){$(this).css('pointer-events','none')}但您需要防止键盘事件也要完全禁用它们。 So if you can disable these elements then go for it. 因此,如果您可以禁用这些元素,那就去吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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