0

我正在使用 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");
    }

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

4

2 回答 2

0

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

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

同样在 html 文件中更改:

<body onload="onLoad();">

编辑:将第三个参数更改addEventListener为“ true”。

于 2013-05-09T10:16:20.953 回答
0

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

于 2013-05-09T11:19:33.737 回答