0

我的 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”,触摸后退按钮
alert("back") 和 alert("TEST") ---> fire

如果将页面更改为“childrenPage”,请触摸后退按钮
警报(“返回”)---> 不触发
警报(“TEST”)---> 触发

我怎样才能做到这一点?我希望用“childrenPage”触发后退按钮事件

4

1 回答 1

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>
于 2012-05-16T11:25:30.957 回答