如何覆盖事件侦听器中的事件阻止默认值



我有一个这样的脚本:

$(document).ready(function(){
window.addEventListener('mousewheel', function(e){
e.preventDefault();
},{passive:false});
});

所以现在我需要做另一个功能,当点击一个按钮时,覆盖上面的脚本并";返回";默认行为。

我测试了这个:

$('#button').on('click', function(){
window.off('mousewheel');
});

它不起作用。

编辑2:

刚刚测试了这个,但仍然不起作用:

$('#button').on('click', function(){
window.removeEventListener('mousewheel', function(e){
return true;
}, true);
});

工作正常!

$(document).ready(function() {
function foo(e) {
console.log("mousewheel");
e.preventDefault();
}
window.addEventListener('mousewheel', foo, true);
$('#button').on('click', function() {
console.log("click");
window.removeEventListener('mousewheel', foo, true);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="button">Button</button>

最新更新