简体   繁体   中英

Reverse e.preventdefault?

I have the following code:

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

Its function is to prevent scrolling when a panel is open. On panel close, I want to unbind the preventdefault and reenable touchstart and touchmove.

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

Use .off() to Remove an event handler.

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

.off() is what you are looking for

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

Assuming you are using jQuery>=1.7, use .on() and .off() . You can use name spaced event names to register/un-register the handlers because else simply calling .off('touchstart touchmove') will remove all other touchstart and touchmove events as well which might have been registered by somebody else.

$(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');
<script>
    // Get touch move event from IOS
    document.ontouchmove = function (event) {
        if (!event.elementIsEnabled)
            event.preventDefault();
    };

    // Get touch move event from IOS
    function enableOnTouchMove(event) {
      event.elementIsEnabled = true;
    };
</script>

then enable ontouchmove on every tag you want. ie:

<div ontouchmove="enableOnTouchMove(event)" id="listing">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM