简体   繁体   English

如何检测手机中是否按下了后退按钮

[英]How to detect if the back button is pressed in mobile phone

Please how can I detect if the back button is been pressed in a mobile phone as shown in the image below 请如何检测手机中是否按下了后退按钮,如下图所示 在此处输入图片说明

I tried using the normal way its been done on desktop like this 我尝试使用通常的方式在台式机上完成

$(document).keypress(function(e){
    if(e.keyCode == 8){
        alert();
    }
});

But it's not working on mobile phones. 但这不适用于手机。 Please anyone with a better clue? 请有更好线索的人吗?

One option is to use jquery mobile. 一种选择是使用jquery mobile。

Here is a code sample (of jquery mobile) 这是(jQuery Mobile的) 代码示例

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

Another option would be to add a 'hardwareBackPress' event listener, as demonstrated in this react-native code sample 另一个选择是添加一个“ hardwareBackPress”事件侦听器,如此本地反应代码示例所示

According to this source , to detect the 'back' key, KEYCODE_BACK = 4 on Android. 根据此消息来源 ,要检测“后退”键,在Android上为KEYCODE_BACK = 4。

Alternatively, you could integrate mobile-detect.js 或者,您可以集成mobile-detect.js

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM