我正在使用 Cordova version 3.6 来创建我的第一个移动应用程序 . 我想使用设备的后退按钮在到达主页时退出应用程序 . 我经历了以下资源但是无法达到目的 .
http://cordova.apache.org/docs/en/3.6.0/cordova_events_events.md.html#backbutton
PhoneGap - android exit on backbutton
backbutton confirm exit app android + phonegap + jquery
我的代码:
JS:
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
function onBackKeyDown() {
document.addEventListener("backbutton", function(e){
if($('body').is('#main')){ //main is the home page id..
e.preventDefault();
navigator.app.exitApp();
} else {
navigator.app.backHistory()
}
}, false);
}
HTML:
感谢您的时间和努力 .
2 回答
在事件处理程序中再次向后退按钮添加侦听器没有多大意义 . 所以从
onBackKeyDown()
删除document.addEventListener
. 并将事件传递给函数 .你必须添加
<script type = "text/javascript" src = "yourpath/cordova.js"> </ script> 你的HTML文件 . 为您的路径键入true place