onDeviceReady不触发PhoneGap hello世界应用程序 [英] onDeviceReady not firing in PhoneGap hello world app
本文介绍了onDeviceReady不触发PhoneGap hello世界应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试做一个简单的警报('test')应用程序,但事件并未被触发, code> function onLoad(){
document.addEventListener(deviceready,onDeviceReady,false);
}
//加载Cordova并且现在可以安全地调用Cordova方法
//
function onDeviceReady(){
alert(' omar');
}
HTML:
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< title> Hello World< / title>
< / head>
< body>
< div class =app>
< h1> AAAA< / h1>
< / div>
< script type =text / javascriptsrc =cordova-2.2.0.js>< / script>
< script type =text / javascriptsrc =js / index.js>< / script>
< / body>
< / html>
为什么是这样?
方案
正确的方式是在添加事件侦听器之前确保文档已完全加载。
示例:
HTML:
< body onload =onLoad>
JS:
function onLoad(){
document.addEventListener(deviceready,onDeviceReady,false);
}
function onDeviceReady(){
// deviceready触发后你想做的任何事情
}
使用jQuery,您可以使用 $(document).ready()
< body onload =onLoad()>
示例:
$(document).ready(){
document.addEventListener(deviceready,onDeviceReady,false);
}
function onDeviceReady(){
// deviceready触发后你想做的任何事情
}
I'm trying to do a simple alert('test') app, but the event isnt being fired, this is the code:
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>
Why is this?
解决方案
The correct way is to make sure that document has completely loaded before adding the event listener.
Example:
HTML:
<body onload="onLoad">
JS:
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
//anything you want done after deviceready has fired
}
With jQuery you can use $(document).ready()
instead of <body onload="onLoad()">
Example:
$(document).ready() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
//anything you want done after deviceready has fired
}
这篇关于onDeviceReady不触发PhoneGap hello世界应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文