Javascript 如何在MIcrosoft Edge中禁用鼠标和键盘的右键单击

Javascript 如何在MIcrosoft Edge中禁用鼠标和键盘的右键单击,javascript,if-statement,microsoft-edge,Javascript,If Statement,Microsoft Edge,在Microsoft Edge中禁用右键单击。下面的代码在Google和Internet Explorer中运行良好 document.onmousedown=单击fn function clickfn(e) { var button; if (navigator.appName == "Microsoft Internet Explorer") { button = event.button; }

在Microsoft Edge中禁用右键单击。下面的代码在Google和Internet Explorer中运行良好 document.onmousedown=单击fn

    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>
功能点击fn(e){
var按钮;
如果(navigator.appName==“Microsoft Internet Explorer”){
按钮=event.button;
}
否则{
按钮=e.按钮;
}
如果(按钮==2){
警报(“右键单击禁用”);
如果(navigator.appName==“Microsoft Internet Explorer”){
event.returnValue=false;
}                
返回false;
}
}

此代码将禁用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
    };
});

为什么要禁用右键单击?这对用户不太友好。在我的场景中,我向用户显示PDF文档。我不希望用户通过鼠标或键盘右键单击来保存为文档。我在Google和IE中找到了禁用右键单击的代码。但在Microsoft中不起作用Edge@K.HariHaran为什么你要强迫用户进入开发者工具来下载PDF?这对用户不太友好。我只想禁用Microsoft Edge、Google、IE中的右键单击功能,只在我的C#项目中使用。@Phihag Chrome和Firefox都有办法破坏您当前的解决方案;通过设置或扩展。如果您想在PDF文件上应用某种DRM,请考虑这样做。