jQuery Mobile - 后退(硬件)按钮无法在应用程序中运行

时间:2011-12-14 20:19:41

标签: jquery jquery-mobile

我是jQmobile的新手,并且一直关注jqMobile网站上的示例。使用多页模板格式,我试图让后退按钮工作(硬件按钮)。但是,当我在第二页并按下(硬件)后退按钮时,它只是退出应用程序,而不是返回到第一页。这是我正在使用的示例代码:

    <head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"> 

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>

<script type="text/javascript">
//This is your app's init method. Here's an example of how to use it
function init() {

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

} 

function onDR(){
   //document.addEventListener("backbutton", backKeyDown, true);
   navigator.notification.alert("PhoneGap is working");
    //boot your app...

}

function backKeyDown() { 

    // do something here if you wish    
}



$(document).bind("mobileinit", function(){
  $.mobile.touchOverflowEnabled = true;
  $.mobile.defaultPageTransition = 'fade';
});



</script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
</head>

<!-- Start of first page -->
<div data-role="page" id="foo">

    <div data-role="header">
        <h1>Foo</h1>
    </div><!-- /header -->

    <div data-role="content">   
        <p>I'm first in the source order so I'm shown as the page.</p>      
        <p>View internal page called <a href="#bar">bar</a></p> 
    </div><!-- /content -->

    <div data-role="footer">
        <h4>Page Footer</h4>
    </div><!-- /footer -->
</div><!-- /page -->


<!-- Start of second page -->
<div data-role="page" id="bar">

    <div data-role="header">
        <h1>Bar</h1>
    </div><!-- /header -->

    <div data-role="content">   
        <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my ID is beeing clicked.</p>      
        <p><a href="#foo">Back to foo</a></p>   
    </div><!-- /content -->

    <div data-role="footer">
        <h4>Page Footer</h4>
    </div><!-- /footer -->
</div><!-- /page -->
</body>

提前感谢所有回复。

1 个答案:

答案 0 :(得分:1)

您的设备可能不支持hashchange事件。

您可以检查设备与用于更新哈希的hashchange事件的兼容性:

if ("onhashchange" in window) {
  //...
}

来源:jQuery - hashchange event

来自jQuery Mobile Docs:

  

独立于点击发生的哈希更改,例如用户时   单击后退按钮,通过hashchange事件处理,   使用Ben Alman的hashchange绑定到window对象   特殊事件插件(包含在jQuery Mobile中)。当哈希改变时   发生(以及第一页加载时)hashchange事件   handler会将location.hash发送到$ .mobile.changePage()   函数,它反过来加载或显示引用的页面。

来源:http://jquerymobile.com/test/docs/pages/page-navmodel.html

相关问题