简体   繁体   English

为了选择或聚焦对象,如何使右键单击的行为与单击鼠标左键的行为相同

[英]How can I make a right-click behave as a left-click for the purpose of selecting or focusing an object

Event though a right-click (as far as I know) fires a mousedown event, that mousedown seems to be ignored in most cases. 尽管右键单击事件(据我所知)触发了mousedown事件,但在大多数情况下,mousedown似乎被忽略了。 I'm currently working on displaying a custom context menu via a right-click, but I'd also like to be able to select an option from a list while I'm right-clicking. 我目前正在通过右键单击显示自定义上下文菜单,但是我还希望能够在右键单击时从列表中选择一个选项。 As of right now my recognizes the click from both buttons enough to run some javascript tied to the onmousedown attribute but not enough to select the option the mouse is over when the mousedown comes from the right button. 截至目前,我认识到来自两个按钮的单击足以运行与onmousedown属性关联的一些javascript,但不足以选择当鼠标右键来自右键时鼠标悬停的选项。

Is there a way to bypass a browser's default behavior of ignoring the mousedown event of a right-click or fool it into thinking the mousedown was generated by the left button instead? 有没有一种方法可以绕过浏览器的默认行为,即忽略鼠标右键单击的mousedown事件,或者愚弄它以为mousedown是由左按钮生成的呢?

Thanks in advance. 提前致谢。

You can use the oncontextmenu event. 您可以使用oncontextmenu事件。

Edit: To simulate the default click behavior on an <option> during a right mouse click, put this code in your event handler when handling right click: 编辑:要在右键单击期间模拟<option>上的默认单击行为,请在处理右键单击时将此代码放在事件处理程序中:

clickedOption.parentNode.selectedIndex = clickedOption.index;

If you are willing to use jQuery , you can simply use mousedown: 如果您愿意使用jQuery ,则只需使用mousedown即可:

$(document).bind('contextmenu', function(event) 
{
   // on right click
   if (event.which == 3)
   {
      // prevent right click from being interpreted by the browser:
      event.preventDefault(); 

      $(document).mousedown(); // simulate left click
   }
});

Of course you can use a fitting selector. 当然,您可以使用配件选择器。 This is great since that way, the right click is only serving as a left click on certain elements of your website. 这样做非常好,因为右键单击仅用作网站某些元素的左键单击。 That way, it's still possible to use the mouse as expected most of the time (depending on your selectors). 这样,大多数时间仍然可以按预期使用鼠标(取决于选择器)。

EDIT: better example 编辑:更好的例子

<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script>
    $(document).ready(function() {
      $("#rightclick").bind('contextmenu', function(event) {

         // on right click
         if (event.which == 3)
         {
            // prevent right click from being interpreted by the browser:
            event.preventDefault(); 
            $(this).click(); // simulate left click
         }
      });

      $('#rightclick').click(function() {
        $(this).html("i have been clicked!");
      });
    });
  </script>
</head>
<body>
  <p>This is a web page.</p>

  <div id="rightclick" style="width:200px; height:100px; background-color: orange">
    Click me with the right or left mouse button. Both will work!
  </div>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM