phonegap 1.5& jquery mobile,backbutton event not fire(Android)?

时间:2012-05-16 09:55:22

标签: jquery mobile cordova back-button

我的index.html代码是

<div data-role="page" id="indexPage">
  <ul data-role="listview"> 
    <li>
        <a href="#childrenPage" >
            <h3>TITLE</h3>
        </a>
    </li>
</ul>
</div>

<div data-role="page" id="childrenPage">
    ****content word****
</div>

我的js代码是

$(document).on("pageinit", function(){
    document.addEventListener("deviceready", function(){            
       document.addEventListener("backbutton", function(){ alert("back") }, false);
             alert("TEST")
    })
});

如果页面是“indexPage”,请触摸后退按钮
警报(“后退”)和警报(“测试”)---&gt;火

如果更改为“childrenPage”,请触摸后退按钮
警报( “返回”)---&GT;不火了 警报( “TEST”)---&GT;火

我该怎么做? 我希望backbutton事件与“childrenPage”火灾

1 个答案:

答案 0 :(得分:0)

这样做

<script type="text/javascript" charset="utf-8">

function onLoad() {
      document.addEventListener("deviceready", onDeviceReady, false);
}

// Cordova is loaded and it is now safe to call Cordova methods
//
function onDeviceReady() {
    // Register the event listener
    document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
  alert("back");
  var currentPageId = $.mobile.activePage.attr('id');
    if(currentPageId == 'indexPage') {
      alert("TEST");
    } else if(currentPageId == 'childrenPage') {
      $.mobile.changePage("#indexPage", {transition : "slide"});
    }
 }
</script>
<body onload="onLoad()">
<div data-role="page" id="indexPage">
</div>

<div data-role="page" id="childrenPage">
</div>
</body>