开发者

How to trap right-click event?

开发者 https://www.devze.com 2022-12-13 13:36 出处:网络
What is the way to trap a right-cli开发者_如何学JAVAck event on a document element? I could not find any event handlers anywhere.Right-click is special on many browsers, triggering the contextmenu eve

What is the way to trap a right-cli开发者_如何学JAVAck event on a document element? I could not find any event handlers anywhere.


Right-click is special on many browsers, triggering the contextmenu event rather than a click event. Some browsers let you prevent the default behavior, some (Opera, for instance) do not. More here: http://unixpapa.com/js/mouse.html

EDIT: Rereading that page (it'd been a while), it looks like mousedown and mouseup are even more reliable than contextmenu (although all major browsers trigger contextmenu). click, on the other hand, doesn't appear to happen at all, on any significant browser.


I think there is event "oncontextmenu", you can hook it.

Here is the jQuery based contextmenu handler,

http://www.trendskitchens.co.nz/jquery/contextmenu/

PS:It doesn't work in My Opera though.


You can use the

window.oncontextmenu

An event handler property for right-click events on the window.

If you need to disable the right click in a page then you can use something like this

window.oncontextmenu = function () {
   return false;
}

or if you need to give your own custom context menu then also you can code inside the function.


You probably want the click or mousedown/up event. From quirksmode:

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
}


The event has a "button" attrubute so lmb is 0 mmb is 1 rmb is 2

http://www.w3schools.com/jsref/event_button.asp

0

精彩评论

暂无评论...
验证码 换一张
取 消