使用javascript禁用移动设备上的长按操作

时间:2013-06-25 12:57:50

标签: javascript android ios jquery-mobile web-applications

在这期间,我制作了一些新的webApps,但拖放时遇到了很大的问题。 我用javascript写了一个文件管理器,但是当我在手机上(智能手机,平板电脑和i或iOs)我尝试拖放工作时,手机会向我显示复制网址或图片的longPress菜单(在文件夹图标上用于examample)。 JS中有一些方法可以在移动设备上禁用longPress吗?

通过css加载图片,对我来说不是一个有效的解决方案。

2 个答案:

答案 0 :(得分:6)

-webkit-touch-callout: none;                /* prevent callout to copy image, etc when tap to hold */
    -webkit-user-select: none;                  /* prevent copy paste, to allow, change 'none' to 'text' */

答案 1 :(得分:0)

检查navigator.userAgent的值并与android | iphone | ipad等进行比较,并在您的java脚本中执行以下操作

function init() {
  onLongPress(document.getElementById('element'));
}

function onLongPress(node) {
  node.ontouchstart = nullEvent;
  node.ontouchend = nullEvent;
  node.ontouchcancel = nullEvent;
  node.ontouchmove = nullEvent;
}

function nullEvent(event) {
  var e = event || window.event;
  e.preventDefault && e.preventDefault();
  e.stopPropagation && e.stopPropagation();
  e.cancelBubble = true;
  e.returnValue = false;
  return false;
}
相关问题