我有一些代码可以显示/隐藏输入按钮,具体取决于条件:
settings = settingsManager.readSettings();
if (settings) {
$("#settings-back-button").show();
} else {
$("#settings-back-button").hide();
}
但是,这并不妨碍用户按下设备后退按钮。我怎么能这样做?
settings = settingsManager.readSettings();
if (settings) {
$("#settings-back-button").show();
disableDeviceBackButton(); // How to implement this?
} else {
$("#settings-back-button").hide();
reEnableDeviceBackButton(); // How to implement this?
}
答案 0 :(得分:1)
我认为此代码应该有效:
<!DOCTYPE html>
<html>
<head>
<title>Back Button Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var backButtonDisabled = false;
// Wait for device API libraries to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown(e) {
if (backButtonDisabled) {
e.preventDefault();
}
}
function disableDeviceBackButton() {
backButtonDisabled = true;
}
function reEnableDeviceBackButton(){
backButtonDisabled = false;
}
</script>
</head>
<body onload="onLoad()">
</body>
</html>
它可能有一些改进,比如不使用全局变量(backButtonDisabled),你可以使用sessionStorage,但重要的部分是e.preventDefault();
避免后退按钮默认行为当你不想要回去。