问题
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