之间的PhoneGap的&QUOT的关系; onBodyLoad()/ onDeviceReady()"功能和jQuery的" $(文件)。就绪()" [英] The relationship between Phonegap's "onBodyLoad()/onDeviceReady()" functions and Jquery's "$(document).ready()"

查看:350
本文介绍了之间的PhoneGap的&QUOT的关系; onBodyLoad()/ onDeviceReady()"功能和jQuery的" $(文件)。就绪()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中使用PhoneGap的+ jQuery Mobile的,我很困惑的的PhoneGap的onBodyLoad()/ onDeviceReady()函数和jQuery的$(文件)。就绪()。

I am using PhoneGap + jQuery Mobile in Android, I am confused about the Phonegap's "onBodyLoad()/onDeviceReady()" functions and Jquery's "$(document).ready()".

在PhoneGap的文件:

In PhoneGap documents:

PhoneGap的由两个code碱基:   本地和JavaScript。虽然   本土code加载,自定义   加载图像显示。然而,   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.

通常情况下,你将要附加一个   事件侦听器   document.addEventListener一旦   HTML文档的DOM加载完毕。

Typically, you will want to attach an event listener with document.addEventListener once the HTML document's DOM has loaded.

在jQuery的文档:

In jQuery doc:

虽然JavaScript提供负载   事件执行code当一个页面   呈现,这一事件并没有得到   触发直到所有资产   图像已被完全接收。   在大多数情况下,脚本可以运行   一旦DOM层级一直   全面建成。该处理器通过   到。就绪()是保证   在DOM后执行已准备就绪,那么   这通常是最好的地方   附上所有其他的事件处理程序和   运行其它jQuery的code。当使用   依赖于CSS的价值脚本   样式属性,是很重要的   引用外部样式或   内嵌式的元素之前   引用脚本。

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.

在那里code依赖于负载情况下,   资产(例如,如果尺寸   图像的需要),在code   应放置在一个处理程序   加载事件代替。

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.

我的实验表明,准备好()总是早些时候onDeviceReady(),这怎么解释?我应该如何使用呢?我应该把准备()的onDeviceReady(),使每一个电话安全吗?

My experiments shows that ready() is always earlier that onDeviceReady(), how to explain this? How should I use them? Should I put ready() in onDeviceReady() to make every call safe?

感谢你在前进。

问候, 克里斯

推荐答案

$(文件)。就绪总是首先开火,因为它被触发时,所有的DOM元素有加载。图像,JavaScript函数,CSS等可能不被这个时候已经加载。

$(document).ready will always fire first because it is triggered when all the DOM elements have loaded. Images, javascript functions, css, etc. may not have loaded by this time.

所以,PhoneGap的拥有你把的onload 对人体的的onLoad 方法方法,以便它触发时特定部分的DOM已准备就绪。一旦DOM是prepared,您将创建一个事件侦听器,以确保phonegap.js的自身的准备(而不仅仅是应用程序用户界面,例如)。只有phonegap.js加载后,您可以使用它提供。

So PhoneGap has you put the onload method on the body's onLoad method so that it fires when that particular part of the DOM is ready. Once the DOM is prepared, you create an event listener to ensure that phonegap.js itself is ready (and not just the application UI, for example). Only after phonegap.js is loaded can you use the functions that it provides.

所以,是的, $(文件)。就绪将首先开火,但这并不意味着你可以使用phonegap.js(以下简称API调用)。你不能把 $(文件)。就绪的另一个函数内(据我所知),因为它是由DOM加载触发。您的可以的(但不应该)却称,从 $你的 onDeviceReady 函数(文件)。就绪。这样做的问题是,如果该设备实际上不是准备好了,API调用将无法进行。

So yes, $(document).ready will fire first, but that does not mean that you can use phonegap.js (the 'api' calls). You cannot put $(document).ready inside of another function (as far as I know), since it is triggered by the DOM being loaded. You can (but should not) however call your onDeviceReady function from $(document).ready. The problem with this is that if the device is NOT in fact ready, the api calls will not be made.

所以,我会继续用身体的onLoad / onDeviceReady链,他们已成立。让我知道这需要更多的阐述。

So I would continue to use the body onLoad/onDeviceReady chain they have set up. Let me know if this needs more elaboration.

这篇关于之间的PhoneGap的&QUOT的关系; onBodyLoad()/ onDeviceReady()"功能和jQuery的" $(文件)。就绪()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆