gpt4 book ai didi

javascript - onDeviceReady 未在 PhoneGap hello world 应用程序中触发

转载 作者:行者123 更新时间:2023-11-30 10:37:11 24 4
gpt4 key购买 nike

我正在尝试做一个简单的 alert('test') 应用程序,但事件没有被触发,这是代码:

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

// Cordova is loaded and it is now safe to make calls Cordova methods
//
function onDeviceReady() {
alert('omar');
}

HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>AAAA</h1>
</div>
<script type="text/javascript" src="cordova-2.2.0.js"></script>
<script type="text/javascript" src="js/index.js"></script>

</body>
</html>

这是为什么?

最佳答案

正确的方法是在添加事件监听器之前确保文档已经完全加载。

例子:

HTML:

<body onload="onLoad">

JS:

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

function onDeviceReady() {
//anything you want done after deviceready has fired
}

通过 jQuery,您可以使用 $(document).ready()而不是 <body onload="onLoad()">

例子:

$(document).ready() {
document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
//anything you want done after deviceready has fired
}

关于javascript - onDeviceReady 未在 PhoneGap hello world 应用程序中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13311805/

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