.live("pageshow"..)没有在Phonegap JQuery Mobile上工作

mra*_*aty 5 jquery-mobile cordova

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

以下所有代码都适用于Chrome.我可以看到控制台日志.但是,当我将其作为Phonegap应用程序的一部分运行时,我从未触发"pageshow"事件.

一直在研究这一段时间但没有成功.希望有人可以解释为什么我的事件丢失,以及使这些代码在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>
Run Code Online (Sandbox Code Playgroud)

details.js

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

小智 6

您应该使用以下内容

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

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

因此,对于设备就绪事件,它将是这样的:

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

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

希望这有帮助,迈克