【问题标题】:how to prevent popup window on right mouse click using javascript?如何使用javascript防止鼠标右键单击弹出窗口?
【发布时间】:2017-02-23 06:54:41
【问题描述】:

我需要使用 javascript 在鼠标右键单击时禁用弹出窗口。因为我想在右键单击时打开另一个弹出窗口。

有什么帮助吗?

【问题讨论】:

    标签: javascript popup right-click


    【解决方案1】:

    请尝试一下:

    $(document).ready(function()
    { 
        $(document).bind("contextmenu",function(e){
            return false;
        }); 
    });
    

    实际上,当您无法在网站上使用鼠标右键时,这很烦人。

    您可以以相同的方式为任何特定块禁用它:

    $(document).ready(function()
    { 
        $('#test').bind("contextmenu",function(e){
            return false;
        }); 
    });​
    

    示例 HTML:

    <div id="test"> ​No right clicks allowed here! </div>​
    

    以及示例 CSS:

    #test { width: 200px; height: 200px; background: red; }
    

    最后是一个例子

    【讨论】:

      【解决方案2】:

      将 JS 文件 here 复制为 menu.js 并将其包含在您的 html 文件路径中。

      menu.css 文件

      ul.SimpleContextMenu {
      display: none;
      position: absolute;
      margin: 0px;
      padding: 0px;
      font-family: verdana;
      font-size: 12px;
      list-style-type: none;
      border-top: 1px solid #000000;
      border-left: 1px solid #000000;
      border-right: 1px solid #000000;
      }ul.SimpleContextMenu li {
      border-bottom: 1px solid #000000;
      }
      ul.SimpleContextMenu li a {
      display: block;
      //menu width
      width: 200px;
      padding: 2px 10px 3px 10px;
      text-decoration: none;
      //Menu font color
      color: #ff0000;
      //Menu background color
      background: #eeeeee;
      }
      ul.SimpleContextMenu li a:hover {
      text-decoration: none;
      color: #ffffff;
      background: #ff0000;
      }
      

      html文件

      <html>
      <head>
      <link type="text/css" rel="stylesheet" href="menu.css" />
      <script type="text/javascript" src="menu.js"></script>
      <script type="text/javascript">
      SimpleContextMenu.setup({'preventDefault':true}); //prevent default
      SimpleContextMenu.attach('container1', 'CM1');
      SimpleContextMenu.attach('container2', 'CM2');
      </script>
      </head>
      <body>
      <div id="container"><ul id="CM1" class="SimpleContextMenu">
      <li><a href="https://link_1_here">link 1</a></li>
      <li><a href="https://link_2_here">link 2</a></li>
      <li><a href="https://link_2_here">link 3</a></li>
      </ul>
      <div class="container1" style="border: 1px dashed red; margin-top: 30px; height: 50px; background: #f2f2f2;">Right Click Here To See The Menu</div>
      </div>
      </html>
      

      这在 Chrome 和 Mozilla 中运行良好。

      【讨论】:

      • 谢谢。我试过这段代码,但它也阻止了右键单击。我需要右键单击打开自定义弹出窗口
      • 这里有一个教程,其中包含有关如何构建自定义 JavaScript 上下文菜单的分步说明。看看这是否有帮助sitepoint.com/…
      • 我已经使用 SWT。但我需要阻止默认的弹出窗口。有什么想法吗??
      • 我已经用示例代码修改了我的答案。看看它是否有效。
      • 非常感谢。它在 html 页面中工作正常。但是当我在 java 类中使用 javascript 代码时,它就不起作用了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多