jquery mobile, use android hardware back button to close a panel

青春壹個敷衍的年華 提交于 2019-12-13 16:26:55

问题


I created a panel using jqm, and I'd like to be able to use the andoird phones back button to close it when it's open.

For now, when the panel is open and I use the back button, it goes to the previous page, instead of only closing the panel.

Any idea how to do this ? Here's my html page with the panel :

<div id="welcome-page" data-role="page">    
    <a data-role="button" id="open-panel" data-theme="a">Enquiries</a>
</div>  

Here's the structure of my js file for the panel :

$(document).on('pagebeforecreate', '#welcome-page', function(){                
    $('<div>').attr({'id':'mypanel','data-role':'panel','data-position':'right','data-display':'overlay'}).appendTo($(this));
    $(document).on('click', '#open-panel', function(){   
        $.mobile.activePage.find('#mypanel').panel("open");
        populateContactForm('args');
        ...
    }); 
    $(document).on('change', '#mypanel input[type=radio]', function(){ 
    ...
    });
});


$(document).on('pagebeforeshow', function() {   
  $(document).bind('keydown', function(event) {
    alert(event.keyCode);
    if (event.keyCode == 27) {
      event.preventDefault();
      $('#mypanel').panel("close");
    }
  });
});

Thanks


回答1:


You can capture hardware back button as below in javascript:

$(document).bind('keydown', function(event) {
  if (event.keyCode == 27) {
    // Prevent default (disable the back button behavior)
    event.preventDefault();

    // Your code to close panel or show another page or whatever...
    $( '#mypanel' ).panel( "close" );
  }
});


来源:https://stackoverflow.com/questions/18286802/jquery-mobile-use-android-hardware-back-button-to-close-a-panel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!