JQuery document.ready vs Phonegap deviceready [英] JQuery document.ready vs Phonegap deviceready
问题描述
我正在使用jQuery创建一个phonegap应用程序。我很困惑我是否应该把我的整个代码包装在JQuery的 $(document).ready()
如
I am making a phonegap application with jquery. I am confused whether I should wrap my entire code inside JQuery's $(document).ready()
like
$(document).ready(function(){
//mycode
});
或在phonegap的deviceready事件内
or inside the deviceready event of phonegap like
document.addEventListener("deviceready", function(){
//mycode
});
我目前使用 document.ready
我想我可能会遇到问题,如果我尝试访问一些Phonegap API方法在 document.ready
。
I am currently using document.ready
but I think I may encounter problems if I try to access some Phonegap API methods inside document.ready
.
这是
推荐答案
你应该使用deviceready事件来避免有趣的事情
You should use the deviceready event to avoid funny things happening.
文档状态:
这是一个非常重要的事件每个PhoneGap应用程序应该使用。
This is a very important event that every PhoneGap application should use.
PhoneGap包含两个代码库:native和JavaScript。在加载本机代码时,会显示自定义加载图像。但是,JavaScript仅在DOM加载时加载。这意味着您的Web应用程序可能会在加载之前调用PhoneGap JavaScript函数。
PhoneGap consists of two code bases: native and JavaScript. While the native code is loading, a custom loading image is displayed. However, JavaScript is only loaded once the DOM loads. This means your web application could, potentially, call a PhoneGap JavaScript function before it is loaded.
PhoneGap deviceready事件在PhoneGap完全加载后触发。设备启动后,您可以安全地拨打PhoneGap功能。
The PhoneGap deviceready event fires once PhoneGap has fully loaded. After the device has fired, you can safely make calls to PhoneGap function.
通常,您需要附加一个事件侦听器,
Typically, you will want to attach an event listener with document.addEventListener
once the HTML document's DOM has loaded.
请阅读这里的文档:http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html
这篇关于JQuery document.ready vs Phonegap deviceready的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!