javascript - 有什么办法可以暂停所有的事件监听器?
问题描述
有没有办法暂停所有“点击”事件监听器,以便我添加的新点击事件监听器单独工作?我需要稍后恢复所有预先存在的事件侦听器。
谢谢你
编辑:我添加了使用 stopPropogation() 方法添加新单击事件侦听器的结尾。为了恢复所有预先存在的事件监听器,我刚刚删除了新的点击监听器。它工作得很好!
感谢您的支持 !
解决方案
您可以通过一个中心函数路由所有听众,该函数是否应该继续。
let elems = document.querySelectorAll(".thing");
elems.forEach(el => el.addEventListener('click', function(e) {
if (okForMeToRun(e)) {
console.log('I am allowed')
} else {
console.log('I am not allowed');
}
}));
function okForMeToRun(e) {
// in case we need to examine the event that wants to fire...
console.log(e.target.innerText + ' wants to fire ')
if (e.target.classList.contains('goodtogo')) return true;
return false;
}
<button class='thing goodtogo'>Test me </button> <button class='thing'>Then test me </button>
推荐阅读
- python-3.x - python 3.6 因分段错误而意外停止(核心转储)
- java - 来自parcelable arraylist的Recyclerview
- redux - React redux 从列表中删除多个对象
- jenkins - 如何使用 Jenkins Job DSL 克隆/继承作业?
- c# - 如何避免来自 .Net 的 WCF 请求中的类型规范?
- php - 调整大小问题:基于用户输入的动态图像创建:Imagick:PHP:自动调整大小
- javascript - 如何检查每个函数是否都通过了整个数组(javascript/jquery)?
- javascript - 如何在javascript中隐藏前一个元素?
- java - 两个模块中存在包时启动层java.lang.LayerInstantiationException
- node.js - 使用 AWS Auto-Scaling 更新 Node.JS 应用程序