请在您的 PhoneGap 应用程序上运行以下代码。您需要等待 'deviceready' 事件才能使用 PhoneGap API。然后,您应该将事件侦听器添加到任何元素。当 'deviceready' 事件未出现时,请确保您的 cordova.js 是否已加载。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<!--This line should be updated according to your cordova.js version-->
<script src="cordova.js"></script>
<script>
document.addEventListener('deviceready', onDeviceready, false);
function onDeviceready(){
console.log('Now you can use PhoneGap APIs');
document.getElementById('exit_button').addEventListener('click', closeThisApp, false);
console.log('Add click event to the button and call app.exitApp');
function closeThisApp(){
navigator.app.exitApp();
}
}
</script>
</head>
<body>
<button id="exit_button">Exit</button>
</body>
</html>