在“PhoneGap + jQuery Mobile”应用程序中正确注册事件 [英] Correct event registration in the 'PhoneGap + jQuery Mobile' application
问题描述
我想在 Android 中定义正确的方式来注册 PhoneGap 和 jQuery Mobile 的初始化事件(jQuery-应用程序。
I am trying to define the correct way to register both initialization events (jQuery-style) for PhoneGap and jQuery Mobile in an Android application.
在学习了文档之后,我想出了以下内容:
After studying the documentation, I came up with the following:
$('#index-page').live('pageinit', function () { // <-- fires
$(document).bind('deviceready', function () { // <-- !fires
// ...
});
});
outer事件( pageinit
)火灾和内部( deviceready
)不... ...
The "outer" event (pageinit
) fires and the "inner" (deviceready
) does not...
完美:
window.addEventListener('load', function () {
document.addEventListener('deviceready', function () {
// ...
}, false);
}, false);
任何人都可以解释第一种类型的事件注册有什么问题?哪种类型更好?
Can anybody explain what's wrong with the first type of event registration? What type is better?
先决条件:
- PhoneGap v1.2
- jQuery Mobile v1.0rc2
- Eclipse v3.7.1
推荐答案
请坚持使用最后一个,因为这是PhoneGap推荐的,你的第一个方法可能不是因为你绑定 deviceready
太晚了(ie:它已经在绑定之前被触发)。这是因为 pageinit
被触发的相对较晚。
Please stick with the last one because this is recommended by PhoneGap, your first approach probably isn't working because you are binding deviceready
too late (ie: it is already fired before your bind). Thats because pageinit
is fired relatively late.
你可以做的是jQuery的方式:
What you can do is the jQuery way:
$(window).load(function() {
$(document).bind('deviceready', function () {
// ...
});
});
这篇关于在“PhoneGap + jQuery Mobile”应用程序中正确注册事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!