javascript - 使用 jQuery Mobile 和 $(document).ready 与 PhoneGap Build 和 document.addEventListener ("deviceready")

标签 javascript jquery jquery-mobile cordova phonegap-build

我已经完成了与此相关的两个问题的解决方案 - JQuery document.ready vs Phonegap devicereadyCorrect way of using JQuery-Mobile/Phonegap together? 我仍然离解决方案还很远。

我们的问题是 PhoneGap Build 插件。我们正在使用 Device 插件将特定的 UI 组件位置和大小定位到 iPhone 4 和 5。为此,我们需要调用事件监听器,使用 Device 插件获取设备型号,设置变量 (iOSversion = true)然后在构建 UI 时使用该变量。

我们无法让它工作。

我们正在使用 jQuery Mobile,所以一切都在一个

 $(document).ready(function(

这有

document.addEventListener("deviceready", false); 

在其中,以及我们其余的脚本和 UI 初始化脚本。 jQM 是模板,但我们正在构建基于 JSON 数据的内容和 UI 组件。到目前为止,一切都很好。

问题是 UI 初始化脚本似乎在事件监听器完成它所做的任何事情之前就开始了,因此 iOS 版本在 UI 脚本运行时永远不会为真。

是的,我们可以在 onDeviceReady 中调用它:

 document.addEventListener("deviceready", onDeviceReady, false);

但是我们无法在浏览器中进行测试。

我确信我缺少一些简单的解决方案,但我们已经研究了一天,这是最后的障碍,让每个人都非常沮丧。

最佳答案

我得到了答案。

window.cordova 在 Phonegap Build 中公开。所以我检查它是否存在,在 jQuery $(document).ready 中是这样的:

if ( !!window.cordova ) {
 // phonegap script has loaded so have our 
 // initializeApplication called when the device is ready
 document.addEventListener("deviceready", 
            initializeApplication, false);
}
else {
 // running in browser without phonegap so 
 // manually call initializeApplication
 initializeApplication();
}
function initializeApplication() {
 // do everything here to initialize the
 // application and its UI
}

关于javascript - 使用 jQuery Mobile 和 $(document).ready 与 PhoneGap Build 和 document.addEventListener ("deviceready"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26640727/

相关文章:

jquery 移动更改复选框颜色

jquery 检查图像是否有标题属性

javascript - 如何在 jQuery 中读取单击事件上第一个 anchor 的数据属性值

javascript - JavaScript 或 JQuery 中 NumberFormat 的等价物是什么?

javascript - 添加类和延迟删除类 JQuery

jquery - css z-index 指针事件

android - 如何改进 Android Eclaire 上的 WebView?

jQueryMobile : Dropdown menus in Header

javascript - 两个回调作为 Array.sort() 的参数,而不是 Array.sort().sort()

javascript - 如何找到非间接/嵌套的父级的子级