有什么方法可以暂停所有“点击”事件侦听器,以便我添加的新点击事件侦听器单独工作?我需要稍后恢复所有预先存在的事件侦听器。
谢谢
编辑: 我添加了使用 stopPropogation() 方法添加新点击事件侦听器的结尾。为了恢复所有预先存在的事件侦听器,我刚刚删除了新的点击侦听器。它工作得很好!
感谢支持!
答案 0 :(得分:0)
您可以通过一个中心功能路由所有听众,该功能限定他们是否应该继续。
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>