【问题标题】:Disable right-click menu in chrome [duplicate]禁用chrome中的右键单击菜单[重复]
【发布时间】:2011-10-11 00:23:27
【问题描述】:

我正在编写一个WebGL 游戏,并希望使用右键单击作为控件。但是,它会弹出一个菜单。是否可以禁用它?我试过了

} 
else if (event.which == 2 || event.which == 3) 
{
    doRightClickControl();
    event.preventDefault();
    event.stopPropagation();

    return false;
}

感谢 dknaack 的提示。我让它像这样工作:

window.oncontextmenu = function(event) {
    event.preventDefault();
    event.stopPropagation();
    return false;
};

【问题讨论】:

    标签: javascript google-chrome


    【解决方案1】:

    仅将 jQuery 用于此目的是多余的。这样就可以了:

    (function () {
      var blockContextMenu, myElement;
    
      blockContextMenu = function (evt) {
        evt.preventDefault();
      };
    
      myElement = document.querySelector('#myElement');
      myElement.addEventListener('contextmenu', blockContextMenu);
    })();
    

    myElement 可以替换为 window.

    【讨论】:

    • 我实际上最终做了这样的事情,没有拉入 JQuery。
    • 对于来自 Google 的普通读者,除非您有充分的理由,否则请不要这样做。 Your users may despise your site.
    • 这在 Chrome 66 中不起作用
    • 正是我想要的!我将它组合成一个语句,并添加了保持转变将让它再次通过。 --- document.querySelector('html').addEventListener('contextmenu', function (evt) { if(!evt.shiftKey){ evt.preventDefault(); } });
    【解决方案2】:

    使用这个:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <head>
    <title>disable rightclick menu - LabLogic</title>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
      document.oncontextmenu=RightMouseDown;
      document.onmousedown = mouseDown; 
    
      function mouseDown(e) {
          if (e.which==3) {//righClick
          alert("Disabled - do whatever you like here..");
       }
    }
    function RightMouseDown() { return false;}
    </script>
    </body>
    </html>
    

    在 chrome 17 中测试,也适用于其他新浏览器

    【讨论】:

    • 在 Firefox 和 chrome 中运行良好,但在 Internet Explorer 8 中运行不正常。测试了它。它在页面上显示错误,然后错误是:这是空还是不是对象..请查看它..谢谢顺便说一句:)
    猜你喜欢
    • 2013-10-05
    • 1970-01-01
    • 2010-09-27
    • 2011-07-19
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多