[英]How to trap right-click event?
What is the way to trap a right-click 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. 右键单击在许多浏览器上都是特殊的,触发contextmenu
事件而不是click
事件。 Some browsers let you prevent the default behavior, some (Opera, for instance) do not. 有些浏览器可以阻止默认行为,有些浏览器(例如Opera)则不会。 More here: http://unixpapa.com/js/mouse.html 更多信息: 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
). 编辑:重读那个页面(已经有一段时间了),它看起来像mousedown
和mouseup
甚至比contextmenu
更可靠(尽管所有主流浏览器都触发了contextmenu
)。 click
, on the other hand, doesn't appear to happen at all, on any significant browser. 另一方面,在任何重要的浏览器上, click
似乎都没有发生。
I think there is event "oncontextmenu", you can hook it. 我认为有事件“oncontextmenu”,你可以勾选它。
Here is the jQuery based contextmenu handler, 这是基于jQuery的contextmenu处理程序,
http://www.trendskitchens.co.nz/jquery/contextmenu/ http://www.trendskitchens.co.nz/jquery/contextmenu/
PS:It doesn't work in My Opera though. PS:虽然它在My Opera中不起作用。
You can use the 你可以使用
window.oncontextmenu 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. 您可能想要click或mousedown / up事件。 From quirksmode : 来自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 该事件有一个“按钮”attrubute所以lmb是0 mmb是1 rmb是2
http://www.w3schools.com/jsref/event_button.asp http://www.w3schools.com/jsref/event_button.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.