如何在MIcrosoft Edge中禁用鼠标右键单击鼠标右键

时间:2017-06-01 14:44:23

标签: javascript if-statement microsoft-edge

禁用Microsoft Edge中的右键单击。以下代码在Google和Internet Explorer中正常运行           document.onmousedown = clickfn;

    function clickfn(e) {
        var button;
        if (navigator.appName == "Microsoft Internet Explorer") {
            button = event.button;

        }
        else {
            button = e.buttons;
        }

        if (button == 2) {
            alert("Right Click Disabled");

            if (navigator.appName == "Microsoft Internet Explorer") {
                event.returnValue = false;
            }                
            return false;
        }
    }
</script>

1 个答案:

答案 0 :(得分:0)

此代码将在IE,edge,chrome,firefox中禁用鼠标右键单击和键盘快捷键

jQuery(document).ready(function() {
    function disableSelection(e) {
        if (typeof e.onselectstart != "undefined") e.onselectstart = function() {
            return false
        };
        else if (typeof e.style.MozUserSelect != "undefined") e.style.MozUserSelect = "none";
        else e.onmousedown = function() {
            return false
        };
        e.style.cursor = "default"
    }
    window.onload = function() {
        disableSelection(document.body)
    };

    window.addEventListener("keydown", function(e) {
        if (e.ctrlKey && (e.which == 65 || e.which == 66 || e.which == 67 || e.which == 70 || e.which == 73 || e.which == 80 || e.which == 83 || e.which == 85 || e.which == 86)) {
            e.preventDefault()
        }
    });
    document.keypress = function(e) {
        if (e.ctrlKey && (e.which == 65 || e.which == 66 || e.which == 70 || e.which == 67 || e.which == 73 || e.which == 80 || e.which == 83 || e.which == 85 || e.which == 86)) {}
        return false
    };

    document.onkeydown = function(e) {
        e = e || window.event;
        if (e.keyCode == 123 || e.keyCode == 18) {
            return false
        }
    };

    document.oncontextmenu = function(e) {
        var t = e || window.event;
        var n = t.target || t.srcElement;
        if (n.nodeName != "A") return false
    };
    document.ondragstart = function() {
        return false
    };
});