gpt4 book ai didi

javascript - Cordova 后退按钮事件从不触发

转载 作者:行者123 更新时间:2023-11-29 20:37:21 25 4
gpt4 key购买 nike

我正在根据文档覆盖我的设备后退按钮,我在 deviceready 事件之后将事件监听器附加到 backbutton,参数为 false

我是这样做的:

// Smart App object
define([
'routes',
'cordova',
'angular',
'angularRoute',
'angularResource',
'angularTouch',
'config',
'controllers',
'services',
'directives',
'helpers'
], function(appRoute) {
var oApp = {
_app: {},

init: function() {
console.log('init');
document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {
console.log('ok device ready');
document.addEventListener('backbutton', function() {
console.error('deviceback start');
angular.element('.ng-scope').scope().back();
console.error('deviceback end');
}, false);
// ...
}
// ...

我疯狂地想弄清楚为什么按下设备按钮没有触发我附加的这个 backbutton 事件,我什至没有在控制台中看到 console.error 消息.我现在正在 Android 上进行测试,我还没有在任何其他手机操作系统上进行过测试。

console.log('ok device ready') 确实触发并且我的应用程序正常工作,设备 backbutton 被覆盖,因为它的默认功能没有发生,但是如前所述,我的功能也没有发生。

我读了其他 stackoverflow topcis,他们说 Cordova 版本得到了修复,但我的 Cordova 版本比他们的版本差很多,我的 .cordova/config.json 文件告诉我:

{
"lib": {
"www": {
"id": "com.tigoenergy.smart",
"version": "3.5.0"
}
}
}

它是 3.5 然后我的 info.txt 告诉我它是 5.0.0:

Node version: v0.10.25

Cordova version: 5.0.0

Config.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<!-- config.xml reference: https://build.phonegap.com/docs/config-xml -->
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"

有没有人有什么想法?

最佳答案

尝试使用使用 cordova 事件的正常方式。

// device APIs are available
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
//Add your back button implementation.
}

官方文档here

修改后的代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>Tigo SMART Installation</title>
<script type="text/javascript" src="apps/installation/js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="apps/installation/js/index.js"></script>
<script type="text/javascript" src="apps/installation/js/fastclick.min.js"></script>
<script type="text/javascript" src="apps/installation/js/sha512.js"></script>
<script type="text/javascript" src="bower_components/uri.js/src/URI.min.js"></script>
<script type="text/javascript" src="js/extlogin.js"></script>
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" type="text/css" href="apps/installation/css/index.css" />

<script type="text/javascript" charset="utf-8">

$(document).ready(function(){
//mycode
console.log('ok loaded');
document.addEventListener("deviceready", onDeviceReady, false);
});
// Wait for device API libraries to load
//
/*function onLoad() {
console.warn('ok loaded')
document.addEventListener("deviceready", onDeviceReady, false);
}
*/

// device APIs are available
//
function onDeviceReady() {
// Register the event listener
console.log('ok device ready');
document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
console.log('ok backey downed');
}

</script>
</head>
<body>

<div ng-view></div>
<script data-main="bin/SmartApp" src="bower_components/requirejs/require.min.js"></script>

<img style="opacity:0;visibility:hidden;" class="loader" src="img/loader.gif" />
</body>
</html>

很少有引用链接herehere

关于javascript - Cordova 后退按钮事件从不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31350619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com