.live(“pageshow”..)在Phonegap JQuery Mobile上不工作 [英] .live("pageshow"..) not working on Phonegap JQuery Mobile

查看:161
本文介绍了.live(“pageshow”..)在Phonegap JQuery Mobile上不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用JQuery Mobile的代码。点这里是,我想要一些代码,当pageshow事件触发时执行。

I have following code that uses JQuery Mobile. Point here is that I want some code to be executed when 'pageshow' event is triggered.

以下所有代码在Chrome上工作得很好。我可以看到控制台日志。但是,当我运行这个作为Phonegap应用程序的一部分,我从来没有得到pageshow事件触发。

All code below works nicely on Chrome. I can see console logs. However when I run this as part of Phonegap application I never get 'pageshow' event triggered.

已经调查了这一段时间,现在没有成功。希望有人可以解释我为什么我的事件缺少和什么是需要的步骤,以获取此代码在Phonegap上工作。

Have been investigating this for some time now without success. Hopefully someone could explain me why my event is missing and what are needed steps to get this code working on Phonegap.

details.html

<!DOCTYPE html>
<html>
<head>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js">      </script>
        <link rel="stylesheet" href="css/index.css" />
        <!-- cordova -->
        <script src="cordova-2.2.0.js"></script>
</head>
<body>
    <div data-role="page" id="details_page">
      <a href="places.html" data-transition="slide" data-direction="reverse">Back</a>
      <h3>Hi I am second page</h3>
    </div>

    <script src="js/details.js"></script>

</body>
</html>

details.js

$("#details_page").live("pageshow", function() {
    console.log('I am alive! (details.html)');
});


推荐答案

您应该使用以下

$(document).delegate("#details_page", "pageshow", function() {
    console.log("Hello world!");
});

另请注意,只能在deviceready事件触发后运行代码。

Also note that you can only run code after the deviceready event has been fired.

所以对于设备就绪事件,它将是这样:

So with the device ready event it would be like so :

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

function onDeviceReady() {
    $(document).delegate("#details_page", "pageshow", function() {
        console.log("Hello world!");
    });
}

希望有帮助,
Mike

Hope this helps, Mike

这篇关于.live(“pageshow”..)在Phonegap JQuery Mobile上不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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