JQuery document.ready 与 Phonegap deviceready [英] JQuery document.ready vs Phonegap deviceready

查看:30
本文介绍了JQuery document.ready 与 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,但我认为如果我尝试访问 document.ready 中的某些 Phonegap API 方法可能会遇到问题.

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.

将我的代码包装在哪个事件中是最好的,document.ready 还是 deviceready?

Which is the best event to wrap my code in, document.ready or deviceready?

推荐答案

你应该使用 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 由两个代码库组成:本机和 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.

通常,一旦 HTML 文档的 DOM 加载完毕,您将希望使用 document.addEventListener 附加一个事件侦听器.

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 与 Phonegap deviceready的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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