【问题标题】:jQuery Mobile - Back (hardware) button not working in appjQuery Mobile - 返回(硬件)按钮在应用程序中不起作用
【发布时间】:2011-12-14 20:19:41
【问题描述】:

我是 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>

提前感谢所有回复。

【问题讨论】:

  • 我认为不可能改变智能手机硬件按钮的行为,特别是使用 javascript.. 和 btw.. 你的 javascript 在哪里?您只发布了标记...
  • Galaxy SII 并在模拟器上进行了测试。我将 javascript 添加到 OP。

标签: jquery jquery-mobile


【解决方案1】:

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

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

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

来源:jQuery - hashchange event

来自 jQuery Mobile 文档:

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

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

【讨论】:

  • 我已经添加了 和 javascript 代码。我在模拟器和我的 Galaxy SII 上都进行了测试,但后退按钮似乎没有按我预期的方式工作。
猜你喜欢
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 2013-06-01
  • 2019-10-16
  • 1970-01-01
相关资源
最近更新 更多