Is right click a Javascript event?

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

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

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

    That is the easiest way to fire it, and it works on all browsers except application webviews like ( CefSharp Chromium etc ... ). I hope my code will help you and good luck!

    const contentToRightClick=document.querySelector("div#contentToRightClick");
    
    //const contentToRightClick=window; //If you want to add it into the whole document
    
    contentToRightClick.oncontextmenu=function(e){
      e=(e||window.event);
      e.preventDefault();
      console.log(e);
      
      return false; //Remove it if you want to keep the default contextmenu 
    }
    div#contentToRightClick{
      background-color: #eee;
      border: 1px solid rgba(0,0,0,.2);
      overflow: hidden;
      padding: 20px;
      height: 150px;
    }
    <div id="contentToRightClick">Right click on the box !</div>

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

    This is worked with me

    if (evt.xa.which == 3) 
    {
        alert("Right mouse clicked");
    }
    
    0 讨论(0)
  • 2020-11-22 10:21

    As others have mentioned, the right mouse button can be detected through the usual mouse events (mousedown, mouseup, click). However, if you're looking for a firing event when the right-click menu is brought up, you're looking in the wrong place. The right-click/context menu is also accessible via the keyboard (shift+F10 or context menu key on Windows and some Linux). In this situation, the event that you're looking for is oncontextmenu:

    window.oncontextmenu = function ()
    {
        showCustomMenu();
        return false;     // cancel default menu
    }
    

    As for the mouse events themselves, browsers set a property to the event object that is accessible from the event handling function:

    document.body.onclick = function (e) {
        var isRightMB;
        e = e || window.event;
    
        if ("which" in e)  // Gecko (Firefox), WebKit (Safari/Chrome) & Opera
            isRightMB = e.which == 3; 
        else if ("button" in e)  // IE, Opera 
            isRightMB = e.button == 2; 
    
        alert("Right mouse button " + (isRightMB ? "" : " was not") + "clicked!");
    } 
    

    window.oncontextmenu - MDC

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

    Easiest way to get right click done is using

     $('classx').on('contextmenu', function (event) {
    
     });
    

    However this is not cross browser solution, browsers behave differently for this event especially firefox and IE. I would recommend below for a cross browser solution

    $('classx').on('mousedown', function (event) {
        var keycode = ( event.keyCode ? event.keyCode : event.which );
        if (keycode === 3) {
           //your right click code goes here      
        }
    });
    
    0 讨论(0)
  • Yes, its a javascript mousedown event. There is a jQuery plugin too to do it

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

    You could use the event window.oncontextmenu, for example:

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

    0 讨论(0)
提交回复
热议问题