Jquery mobile changepage with back button not working

大兔子大兔子 提交于 2019-12-23 10:16:28

问题


I have 4 pages within my JQM main HTML file. When I switch to one using changepage it's fine the first time, but I use a data-rel=back button to go back and this switches to the previous page but then bounces back to the page that has the back button. Should I just not use data-rel=back? If not what alternative is there?

Using JQM 1.3.1

$("#listView").on("vclick","li", function(e) {  
  //ajax call to get results for second page
  $.mobile.changePage('#second');
}

Button on second page

<a href="#" data-rel="back">Back</a>

回答1:


To go to previous page programmatically, use the below code. You need also to use stopImmediatePropagation(); to stop jQuery Mobile from jumping twice, which will result showing the same page.

Edit: I tested it on iPad, preventDefault() is required too.

Demo

$(document).on('vclick', '[data-rel=back]', function (e) {
 e.stopImmediatePropagation();
 e.preventDefault();
 var back = $.mobile.activePage.prev('[data-role=page]');
  $.mobile.changePage(back, { 
    transition: 'slide',
    reverse: true });
});



回答2:


Use this one. You can redirect between pages using location.hash=" " with page id in it.

DEMO http://jsfiddle.net/yeyene/uJz3E/7/

$("#listView").on("vclick","li", function(e) {  
  // second is the page you want to redirect on click.
  location.hash = "second";
});


来源:https://stackoverflow.com/questions/17044824/jquery-mobile-changepage-with-back-button-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!