在PhoneGap文档中:@H_404_3@
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.@H_404_3@The PhoneGap deviceready event fires
once PhoneGap has fully loaded. After
the device has fired,you can safely
make calls to PhoneGap function.@H_404_3@Typically,you will want to attach an
event listener with
document.addEventListener once the
HTML document’s DOM has loaded.@H_404_3@
在jQuery文档:@H_404_3@
While JavaScript provides the load
event for executing code when a page
is rendered,this event does not get
triggered until all assets such as
images have been completely received.
In most cases,the script can be run
as soon as the DOM hierarchy has been
fully constructed. The handler passed
to .ready() is guaranteed to be
executed after the DOM is ready,so
this is usually the best place to
attach all other event handlers and
run other jQuery code. When using
scripts that rely on the value of CSS
style properties,it’s important to
reference external stylesheets or
embed style elements before
referencing the scripts.@H_404_3@In cases where code relies on loaded
assets (for example,if the dimensions
of an image are required),the code
should be placed in a handler for the
load event instead.@H_404_3@
我的实验表明,ready()总是早于onDeviceReady(),如何解释这个?我应该如何使用它们?我应该把onDeviceReady()中的ready()使每个调用安全吗?@H_404_3@
先谢谢你。@H_404_3@
问候,
克里斯@H_404_3@
解决方法
所以PhoneGap你把onload方法放在body的onLoad方法上,这样当DOM的特定部分就绪时就会触发。一旦DOM准备就绪,您将创建一个事件侦听器,以确保phonegap.js本身已准备就绪(例如,而不仅仅是应用程序UI)。只有在phonegap.js加载后,才能使用它提供的函数。@H_404_3@
所以是的,$(document).ready将首先激活,但这并不意味着你可以使用phonegap.js(‘api’调用)。你不能把$(document).ready放在另一个函数(据我所知),因为它是由被加载的DOM触发的。你可以(但不应该)从$(document).ready调用你的onDeviceReady函数。这样做的问题是,如果设备实际上没有准备好,则不会进行api调用。@H_404_3@
所以我会继续使用它们已经设置的body onLoad / onDeviceReady链。让我知道这是否需要更多的阐述。@H_404_3@