【问题标题】:Reverse e.preventdefault?反向 e.preventdefault?
【发布时间】:2014-04-29 00:21:33
【问题描述】:

我有以下代码:

$(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
});

【问题讨论】:

  • 嗯,确实如此,而且确实如此。我关闭面板,无法滚动禁用滚动的主要内容。我为什么要打开这个问题?

标签: javascript jquery events jquery-mobile


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    .off() 就是你要找的东西

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

    【讨论】:

      【解决方案3】:

      假设您使用的是 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')
      });
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-09
          • 1970-01-01
          • 2021-12-13
          • 2012-07-16
          • 2021-12-10
          相关资源
          最近更新 更多