小编典典

在Android上长按时禁用上下文菜单

javascript

我想禁用长按(触摸并按住)Web应用程序中的图像后出现的上下文菜单。我见过关于如何做到这一点的不同想法,但似乎没有一个对我有用。

有没有办法通过HTML / CSS / Javascript在Android上执行此操作?


阅读 226

收藏
2020-05-01

共1个答案

小编典典

这应该适用于1.6或更高版本(如果我没记错的话)。我不认为有1.5或更早版本的解决方法。

<!DOCTYPE html>
<html>
<head>
  <script>
    function absorbEvent_(event) {
      var e = event || window.event;
      e.preventDefault && e.preventDefault();
      e.stopPropagation && e.stopPropagation();
      e.cancelBubble = true;
      e.returnValue = false;
      return false;
    }

    function preventLongPressMenu(node) {
      node.ontouchstart = absorbEvent_;
      node.ontouchmove = absorbEvent_;
      node.ontouchend = absorbEvent_;
      node.ontouchcancel = absorbEvent_;
    }

    function init() {
      preventLongPressMenu(document.getElementById('theimage'));
    }
  </script>
</head>
<body onload="init()">
  <img id="theimage" src="http://www.google.com/logos/arthurboyd2010-hp.jpg" width="400">
</body>
</html>
2020-05-01