逃离 preventDefault 函数

Escape from preventDefault function

我有代码,点击 <a> div 显示。它禁用我的滚动条,当用户单击禁用按钮 (img) 时,我想退出我的 preventDefault 函数,因为当我想使用滚动条时,它再次被禁用。

如您所见,我恢复了默认值 css,因此网站看起来像以前一样,但在鼠标滚轮上,我的滚动条再次被禁用。我正在寻找重置此 preventDefault 或以某种方式删除此功能,我不知道。

$('#region').click(function(e) {
    $('#regions').append("<div class=\"regionWindow\"></div><div class=\"regionCancel\"><img class=\"cancelButton\" src=\"img/cancelButton.png\" /></div>");
    $('.content').css({ "height": "100%", "background": "rgba(0,0,0,0.7)", "pointer-events": "none" });
    $('body').on({
        'mousewheel': function(e) {
            if (e.target.id == 'el') return;
            e.preventDefault();
            e.stopPropagation();
            $(this).css({ "overflow-y": "scroll", "position": "fixed", "width": "100%" });
        }
    });
    $('.cancelButton').click(function(){
        $('.content').css({"height":"","background":"","pointer-events":""});
        $('#regions').remove('div');
        $('body').css({"overflow-y":"","position":"","width":""});
});
});

您首先需要将事件处理程序定义为一个单独的(命名的)函数:

function myMouseWheelHandler(e) {
    if (e.target.id == 'el') return;
    e.preventDefault();
    e.stopPropagation();
    $(this).css({ "overflow-y": "scroll", "position": "fixed", "width": "100%" });
};

然后保留您拥有的 .on(),但现在使用您的命名函数:

$('body').on('mousewheel', myMouseWheelHandler);

然后你可以用.off()删除它:

$('body').off('mousewheel', myMouseWheelHandler);