移动端长按禁止默认事件总结

1、如果是禁用长按选择文字功能,用 css :
全局* 或者 局部选择相映的DOM加

        * {
              -webkit-touch-callout:none;
              -webkit-user-select:none;
              -khtml-user-select:none;
              -moz-user-select:none;
              -ms-user-select:none;
              user-select:none;
        }
但是包含input框时会导致不可用,可以添加下面代码:
input{
  -webkit-user-select: auto;
}

2、如果对图片禁止选中:

img { pointer-events: none; }

3、如果是想禁用长长长长按弹出菜单, 用 js:

    box.addEventListener('contextmenu', function(e){
        e.preventDefault();
     });

4、如果需要禁止的页面没有点击事件,这个会导致短点击事件无效, 可以使用下面这段代码:

    window.ontouchstart = function(e) { 
       e.preventDefault(); 
    };

猜你喜欢

转载自blog.csdn.net/webKris/article/details/80942773
今日推荐