Is right click a Javascript event?

后端 未结 18 2113
南笙
南笙 2020-11-22 10:02

Is right click a Javascript event? If so, how do I use it?

相关标签:
18条回答
  • 2020-11-22 10:28

    If You want to call the function while right click event means we can use following

     <html lang="en" oncontextmenu="func(); return false;">
     </html>
    
    <script>
    function func(){
    alert("Yes");
    }
    </script>
    
    0 讨论(0)
  • 2020-11-22 10:31

    To handle right click from the mouse, you can use the 'oncontextmenu' event. Below is an example:

     document.body.oncontextmenu=function(event) {
         alert(" Right click! ");
     };
    

    the above code alerts some text when right click is pressed. If you do not want the default menu of the browser to appear, you can add return false; At the end of the content of the function. Thanks.

    0 讨论(0)
  • 2020-11-22 10:35

    Ya, though w3c says the right click can be detected by the click event, onClick is not triggered through right click in usual browsers.

    In fact, right click only trigger onMouseDown onMouseUp and onContextMenu.

    Thus, you can regard "onContextMenu" as the right click event. It's an HTML5.0 standard.

    0 讨论(0)
  • 2020-11-22 10:35

    Try using the which and/or button property

    The demo

    function onClick(e) {
      if (e.which === 1 || e.button === 0) {
        console.log('Left mouse button at ' + e.clientX + 'x' + e.clientY);
      }
      if (e.which === 2 || e.button === 1) {
        console.log('Middle mouse button ' + e.clientX + 'x' + e.clientY);
      }
      if (e.which === 3 || e.button === 2) {
        console.log('Right mouse button ' + e.clientX + 'x' + e.clientY);
      }
      if (e.which === 4 || e.button === 3) {
        console.log('Backward mouse button ' + e.clientX + 'x' + e.clientY);
      }
      if (e.which === 5 || e.button === 4) {
        console.log('Forward mouse button ' + e.clientX + 'x' + e.clientY);
      }
    }
    window.addEventListener("mousedown", onClick);
    
    

    Some info here

    0 讨论(0)
  • 2020-11-22 10:36

    window.oncontextmenu = function (e) {
      e.preventDefault()
      alert('Right Click')
    }
    <h1>Please Right Click here!</h1>

    0 讨论(0)
  • 2020-11-22 10:36

    Handle event using jQuery library

    $(window).on("contextmenu", function(e)
    {
       alert("Right click");
    })
    
    0 讨论(0)
提交回复
热议问题