PhoneGap应用程序- deviceready"事件侦听器未被调用

PhoneGap Application - "deviceready" event listener is not being called

本文关键字:侦听器 事件 调用 quot 应用程序 deviceready PhoneGap      更新时间:2023-09-26

我使用vs2012,我正在开发一个PhoneGap应用程序,在该应用程序中,我使用以下JavaScript代码:

document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady()
    {
       // alert("hh");
        console.log("Entering index.html.onDeviceReady");
        //var element = document.getElementById('deviceProperties');
        var html = "";
        html = html + "<li>" + 'Device Name: ' + device.name + "</li>";
        html = html + "<li>" + 'Device Cordova: ' + device.cordova + "</li>";
        html = html + "<li>" + 'Device Platform: ' + device.platform + "</li>";
        html = html + "<li>" + 'Device UUID: ' + device.uuid + "</li>";
        console.log(html);
        $("#deviceProperties").html(html);
        $("#deviceProperties").listview('refresh');
        console.log("Exiting index.html.onDeviceReady");
    }

但是没有调用该函数,也没有动态添加任何元素。我做错了什么?

确保在脚本之前添加脚本type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"

页面必须完全加载才能调用deviceready事件侦听器,否则它将无法工作,因为设备尚未准备好。在你的脚本中,它在页面完全加载之前被调用。试试这个:

function onLoad()
{
   document.addEventListener("deviceready",onDeviceReady, true);
}

同样在html文件中更改:

<body onload="onLoad();">

编辑:将addEventListener中的第三个参数改为" true "