在iOS野生动物园中禁用选择上下文菜单 [英] Disable selection context menu in iOS safari

查看:54
本文介绍了在iOS野生动物园中禁用选择上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用在iOS Safari(网络浏览器)中选择某些文本后出现的默认上下文菜单.有可能吗?

I want to disable the default context menu that appears after a certain text is selected in iOS Safari (web browser). Is that possible?

推荐答案

我发现的唯一方法是删除选择,然后再次使用javascript选择.看看我的代码:

The only way i found was by removing the selection and select again with javascript. Have a look at my code:

/* prevent ios edit-menu */
if (/(iPad|iPhone|iPod)/g.test(navigator.userAgent)) {
    !function(){
        var target = document.body; // the element where the edit menue should be disabled

        var preventSelRecursion;
        document.addEventListener('selectionchange', function(e){
            var S = getSelection();
            if (!S.rangeCount) return;
            if (S.isCollapsed) return;
            var r = S.getRangeAt(0);
            if (!target.contains(r.commonAncestorContainer)) return;
            if (preventSelRecursion) return;
            iosSelMenuPrevent();
        }, false);

        var iosSelMenuPrevent = debounce(function(){
            var S = getSelection();
            var r = S.getRangeAt(0);
            preventSelRecursion = true;
            S = getSelection();
            S.removeAllRanges();
            setTimeout(function(){ // make remove-add-selection removes the menu
                S.addRange(r);
                setTimeout(function(){
                    preventSelRecursion = false;
                });
            },4);
        },800); // if no selectionchange during 800ms : remove the menu

        /* helper-function */
        function debounce(fn, delay) {
            var timer = null;
            return function () {
                var context = this, args = arguments;
                clearTimeout(timer);
                timer = setTimeout(function () {
                    fn.apply(context, args);
                }, delay);
            };
        }
    }();
}

这篇关于在iOS野生动物园中禁用选择上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆