如何在 jquery 中为我的所有页面和元素阻止 F12 键盘键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28575722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:29:22  来源:igfitidea点击:

How can I block F12 keyboard key in jquery for all my pages and elements?

jquery

提问by Internial

I have been trying to stop inspect element to the maximum amount. I know I can't stop them but still I would really like to reduce the chance. So how do I block the F12keyboard key in all HTML elements?

我一直试图将检查元素停止到最大数量。我知道我无法阻止他们,但我仍然很想减少机会。那么如何F12在所有 HTML 元素中屏蔽键盘键呢?

Result: no one can access F12and get inspect element.

结果:没有人可以访问F12和获取检查元素。

回答by Sadikhasan

Here 123is the keyCodeof F12which opens the Inspect Element screen in the browser. Adding a keydownevent than only does return falsefor 123will block the Inspect Element screen.

这里123keyCodeF12这将打开检查元素的屏幕在浏览器中。添加一个keydown事件而不仅仅是return falsefor123将阻止 Inspect Element 屏幕。

$(document).keydown(function (event) {
    if (event.keyCode == 123) { // Prevent F12
        return false;
    } else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) { // Prevent Ctrl+Shift+I        
        return false;
    }
});

Prevent Right Click > Inspect Element

防止右键单击 > 检查元素

$(document).on("contextmenu", function (e) {        
    e.preventDefault();
});

Demo

演示

回答by PK-1825

add below script in your file, in head section after Jquery.js file

在您的文件中添加以下脚本,在 Jquery.js 文件之后的头部部分

     <script language="JavaScript">

       window.onload = function () {
           document.addEventListener("contextmenu", function (e) {
               e.preventDefault();
           }, false);
           document.addEventListener("keydown", function (e) {
               //document.onkeydown = function(e) {
               // "I" key
               if (e.ctrlKey && e.shiftKey && e.keyCode == 73) {
                   disabledEvent(e);
               }
               // "J" key
               if (e.ctrlKey && e.shiftKey && e.keyCode == 74) {
                   disabledEvent(e);
               }
               // "S" key + macOS
               if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
                   disabledEvent(e);
               }
               // "U" key
               if (e.ctrlKey && e.keyCode == 85) {
                   disabledEvent(e);
               }
               // "F12" key
               if (event.keyCode == 123) {
                   disabledEvent(e);
               }
           }, false);
           function disabledEvent(e) {
               if (e.stopPropagation) {
                   e.stopPropagation();
               } else if (window.event) {
                   window.event.cancelBubble = true;
               }
               e.preventDefault();
               return false;
           }
       }
//edit: removed ";" from last "}" because of javascript error
</script>