【问题标题】:Disable longPress action on mobile with javascript使用 javascript 在移动设备上禁用 longPress 操作
【发布时间】:2013-06-22 06:55:47
【问题描述】:

在这期间,我做了一些新的webApps,但是我在拖放方面遇到了很大的问题。 我用 javascript 编写了一个文件管理器,但是当我在移动设备(智能手机、平板电脑、安卓或 iOs)上尝试拖放时,手机会显示 longPress 菜单(例如文件夹图标),用于复制 url 或图像。 JS中有什么方法可以在手机上禁用longPress?

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

【问题讨论】:

  • 你在使用 jQuery Mobile 吗?

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


【解决方案1】:
-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' */

【讨论】:

    【解决方案2】:

    检查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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2017-09-27
      相关资源
      最近更新 更多