如何控制 Jquery mobile 中的后退按钮事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18211984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:12:41  来源:igfitidea点击:

how to control back button event in Jquery mobile?

javascriptjquery-mobilejquerykeycode

提问by Serkan

I tried to control back button but i cant.In here;

我试图控制后退按钮,但我不能。在这里;

Take control of hardware back button jquery mobile

控制硬件后退按钮 jquery mobile

  event.keyCode == 27 //thats for escape 
  event.keyCode == 8 //thats for backspace..its also working on browser but it doesnt work on my tablet.

Any suggestions??

有什么建议??

回答by Omar

Recommended method pagecontainerbeforechange: https://jqmtricks.wordpress.com/2014/12/01/detect-back-navigation/

推荐方法pagecontainerbeforechangehttps: //jqmtricks.wordpress.com/2014/12/01/detect-back-navigation/



You need to listen to navigationevent and state.direction.

您需要收听navigationevent 和state.direction.

$(window).on("navigate", function (event, data) {
  var direction = data.state.direction;
  if (direction == 'back') {
    // do something
  }
  if (direction == 'forward') {
    // do something else
  }
});

jQM API:Navigation event

jQM API:导航事件

Demo

演示

回答by Bashirpour

You can do this without Jquery mobile

你可以在没有 Jquery mobile 的情况下做到这一点

window.addEventListener("hashchange", function(e) {
    if(e.oldURL.length > e.newURL.length)
        alert("back")
});
<a href="#p2">goto page 2</a> and then use browser's navigation.</div>

And also Demo in codepen

还有codepen中的Demo