gpt4 book ai didi

cordova - 奇怪的 phonegap 设备就绪事件行为

转载 作者:行者123 更新时间:2023-12-04 04:42:51 25 4
gpt4 key购买 nike

我正在尝试让设备就绪事件在 phonegap 3 helloworld 默认应用程序上工作。

html

<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>

javascript
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
alert('welcome!');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');

console.log('Received Event: ' + id);
}
};

这段代码运行正常,当我打开应用程序时会触发我的欢迎警报。但是,如果我注释掉其中任何一行:
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>

欢迎警报将不再触发。这里发生了什么我不知道 phonegap 使用什么机制来创建这种行为?

最佳答案

这里的问题不在于 phonegap,而在于 receivedEvent导致错误的函数。如果您移动 alert在调用 receivedEvent 之前,您应该会看到 deviceready事件仍然正确触发。

实际问题是那条线

var listeningElement = parentElement.querySelector('.listening');

获取 <p class="event listening">..</p>来自 DOM 的元素。由于您删除了该元素, listeningElement将是 null .现在电话
listeningElement.setAttribute('style', 'display:none;');

会因此而失败。

关于cordova - 奇怪的 phonegap 设备就绪事件行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18627589/

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