2012-05-16 82 views
0

我的index.html代碼phonegap 1.5&jquery mobile,backbutton event not fire(Android)?

<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」,觸摸後退按鈕
警報( 「回」)和警報( 「TEST」 )--->火

如果changepage爲 「childrenPage」,觸摸後退按鈕
警報( 「返回」)--->不火
警報( 「TEST」)--->網絡連接re

我該怎麼做? 我希望後退按鈕事件火 「childrenPage」

回答

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> 
+0

親愛的主席先生,onBackKeyDown()上childrenPage不火.... – user409329