移动web端禁止长按a标签,弹出“在浏览器中打开”菜单

时间:2018-02-02 14:30:40   收藏:0   阅读:1957

长按press事件会导致浏览器弹出菜单

技术分享图片

方法一:

苹果禁止:

-webkit-touch-callout: none;

 

安卓的不行。禁止弹出只能用js来控制:

$(‘a‘).ontouchstart = function(e) { 
    e.preventDefault(); 
};

 

方法二:

将<a>标签换成其他的标签,如<button>,<div>,<p>。然后绑定touchstart,click事件js跳转。

<div class="jump">链接</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(.jump).on(touchstart, function () {
        location.href = "";
    })
</script>

 

如果想要禁止文本被选中,可以加上css

.jump {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

 

 

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!