当删除html时,Cordova / Phonegap会停止触发

时间:2014-12-23 09:01:40

标签: cordova

我正在使用科尔多瓦,而且我遇到了一个非常奇怪的问题。

我已经向index.js文件添加了提醒,只是为了让我知道onDeviceReady已被解雇。

除非从html页面中删除此代码,否则一切正常:

<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>

这是index.js的代码:

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 explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        alert('I am ready');


    },
    // 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);
    }
};

app.initialize();

以前有人有这个问题吗?我该如何解决?

1 个答案:

答案 0 :(得分:2)

在线

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

您正在使用 listen 类获取div。之后,您尝试在

处为该不存在的div设置属性
listeningElement.setAttribute('style', 'display:none;');

这会导致异常,并且在您发出警报之前执行永远不会继续。