Is right click a Javascript event?

2018-12-31 16:33发布

问题:

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

回答1:

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



回答2:

have a look at the following jQuery code:

$(\"#myId\").mousedown(function(ev){
      if(ev.which == 3)
      {
            alert(\"Right mouse button clicked on element with id myId\");
      }
});

The value of which will be:

  • 1 for the left button
  • 2 for the middle button
  • 3 for the right button


回答3:

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.



回答4:

The my answer is window.oncontextmenu, for example:

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



回答5:

No, but you can detect what mouse button was used in the \"onmousedown\" event... and from there determine if it was a \"right-click\".



回答6:

The following code is using jQuery to generate a custom rightclick event based on the default mousedown and mouseup events. It considers the following points:

  • trigger on mouseup
  • trigger only when pressed mousedown on the same element before
  • this code especially also works in JFX Webview (since the contextmenu event is not triggered there)
  • it does NOT trigger when the contextmenu key on the keyboard is pressed (like the solution with the on(\'contextmenu\', ...) does

$(function ()
{ // global rightclick handler - trigger custom event \"rightclick\"
	var mouseDownElements = [];
	$(document).on(\'mousedown\', \'*\', function(event)
	{
		if (event.which == 3)
		{
			mouseDownElements.push(this);
		}
	});
	$(document).on(\'mouseup\', \'*\', function(event)
	{
		if (event.which == 3 && mouseDownElements.indexOf(this) >= 0)
		{
			$(this).trigger(\'rightclick\');
		}
	});
	$(document).on(\'mouseup\', function()
	{
		 mouseDownElements.length = 0;
	});
    // disable contextmenu
    $(document).on(\'contextmenu\', function(event)
	{
		 event.preventDefault();
	});
});



// Usage:
$(\'#testButton\').on(\'rightclick\', function(event)
{
  alert(\'this was a rightclick\');
});
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>
<button id=\"testButton\">Rightclick me</button>



回答7:

Yes - it is!

function doSomething(e) {
    var rightclick;
    if (!e) var e = window.event;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
    alert(\'Rightclick: \' + rightclick); // true or false
}


回答8:

Yes, oncontextmenu is probably the best alternative but be aware that it triggers on mouse down whereas click will trigger on mouse up.

Other related questions were asking about double right click - which apparently isn\'t supported except through manual timer checking. One reason you might want to be able to have right double click is if you need/want to support left-handed mouse input (button reversal). The browser implementations seem to make a lot of assumptions about how we should be using the available input devices.



回答9:

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      
    }
});


回答10:

Handle event using jQuery library

$(window).on(\"contextmenu\", function(e)
{
   alert(\"Right click\");
})


回答11:

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>


回答12:

This is worked with me

if (evt.xa.which == 3) 
{
    alert(\"Right mouse clicked\");
}


回答13:

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.



回答14:

Yes, its a javascript mousedown event. There is a jQuery plugin too to do it



标签: javascript