4

我有一个有效的 HelloWorld phonegap 程序,其中安装了 jquery mobile,如下所述:http: //jquerymobile.com/demos/1.1.0/docs/about/getting-started.html。我为此添加了一些 javascript 来试验跨域资源共享:

<script>
$(document).bind("pageinit", function() {
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
    $.mobile.changePage("http://jquery.com");
});
</script>

这在模拟器(2.3)上效果很好,jquery.com 是通过 jquery 移动演示加载的。但是,在实际的 2.3 Android 设备(运行 Cyanogen、Galaxy SII、Galaxy Player 的 T-mobile G2)上,changePage() 调用什么也不做。

4

2 回答 2

4

$.mobile.changePage()在函数内调用pageinit函数听起来是个坏主意,因为这会导致无限循环。该$.mobile.changePage()函数初始化指定为target参数的页面,因此每次调用$.mobile.changePage()时也会触发一个pageinit事件。

您可能希望在初始化 jQuery Mobile 之前绑定到mobileinit事件以覆盖$.support.cors变量:

<script src="jquery.js"></script>
<script>
$(document).bind("mobileinit", function() {
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
    $.mobile.changePage("http://jquery.com");
});
</script>
<script src="jquery-mobile.js"></script>

相关文档:

于 2012-07-16T18:33:36.557 回答
1

尝试mobileinit代替pageinit. 因为您绑定的事件是普通的 jQuery,而对于 jQuery mobile,初始化事件是 mobileinit。

The $.mobile.allowCrossDomainPages option must be set before any cross-domain request is made so we recommend wrapping this in a mobileinit handler.

于 2012-07-14T23:02:45.200 回答