25

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

使用 jQuery Mobile 控制硬件后退按钮

  event.keyCode == 27 // That’s for escape
  event.keyCode == 8 // That’s for backspace...it's also working on browser, but it doesn’t work on my tablet.

我该怎么做?

4

2 回答 2

55

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


您需要收听navigation事件和state.direction.

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

jQuery Mobile API: 导航事件

演示

于 2013-08-13T15:36:04.137 回答
3

你可以在没有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>

还有CodePen 中的 Demo

于 2019-05-21T08:16:25.793 回答