Reverse e.preventdefault?

Reverse e.preventdefault?

本文关键字:preventdefault Reverse      更新时间:2023-09-26

我有以下代码:

$(document).bind('panelopen', function (e, data) {     
    $('#ReleaseTransactionsPageContent').on('touchstart touchmove', function(e){ 
         e.preventDefault(); 
    });
});

它的作用是防止面板打开时滚动。在面板关闭,我想取消绑定的preventdefault和重新启用touchstart和touchmove。

$(document).bind('panelclose', function (e, data) {     
    $('#ReleaseTransactionsPageContent')....not sure what to put here
});

使用。off()删除事件处理程序。

$('#ReleaseTransactionsPageContent').off('touchstart touchmove'); //remove previous attached handler
$('#ReleaseTransactionsPageContent').on('touchstart touchmove',function(){ //attach new handler
   //code here
});

.off()就是你要找的

$(document).bind('panelclose', function (e, data) {     
    $('#ReleaseTransactionsPageContent').off('touchstart touchmove');
});

假设您正在使用jQuery>=1.7,请使用。on()和。off()。您可以使用名称间隔的事件名称来注册/取消注册处理程序,因为否则只需调用.off('touchstart touchmove')就会删除所有其他touchstarttouchmove事件,这些事件可能已经被其他人注册了。

$(document).on('panelopen', function (e, data) {
    $('#ReleaseTransactionsPageContent').on('touchstart.ReleaseTransactionsPageContent touchmove.ReleaseTransactionsPageContent', function (e) {
        e.preventDefault();
    });
});
$(document).bind('panelclose', function (e, data) {
    $('#ReleaseTransactionsPageContent').off('touchstart.ReleaseTransactionsPageContent touchmove.ReleaseTransactionsPageContent')
});
  • 事件名称和命名空间

使用off方法删除事件处理程序:

$('#ReleaseTransactionsPageContent').off('touchstart touchmove');