javascript 无法使用 jquery 的单击事件处理程序来检测右键单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7343117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use jquery's click event handler to detect right click
提问by jbyrd
In trying to detect a right mouse click with jquery, I noticed that the click event handler doesn't seem to be fired off with a right mouse click, while the mousedown or mouseup event handler's do.
在尝试使用 jquery 检测鼠标右键单击时,我注意到单击事件处理程序似乎没有通过鼠标右键单击触发,而 mousedown 或 mouseup 事件处理程序则可以。
For example, after a right click on the test div, the following alerts 'testing!':
例如,在测试 div 上右键单击后,以下警告“正在测试!”:
$('#test').mousedown(function(e) {
alert('testing');
});
However, the following does not:
但是,下列情况不:
$('#test').click(function(e) {
alert('testing!');
});
Does anyone know why?
有谁知道为什么?
回答by wesbos
When you mousedown, the even fired has event.which
当你按下鼠标时,即使被解雇了 event.which
Taken from here: How to distinguish between left and right mouse click with jQuery
$('#element').mousedown(function(event) {
switch (event.which) {
case 1:
alert('Left mouse button pressed');
break;
case 2:
alert('Middle mouse button pressed');
break;
case 3:
alert('Right mouse button pressed');
break;
default:
alert('You have a strange mouse');
}
});
So instead of using .click(), use mousedown and check for the cases.
因此,不要使用 .click(),而是使用 mousedown 并检查情况。
回答by htanata
回答by sabertooth1990
I have also tried the following code to catch right mouse click for certain class of elements
我还尝试了以下代码来捕捉某些类元素的鼠标右键单击
$(".brick").mousedown(function (event) {
if (event.which === 3) {
currentRightClickedTileID = $(this).attr("id");
}
});
This code doesn't always catch the right click.
此代码并不总是能捕捉到右键单击。
回答by nnnnnn
Not sure which browser(s) you've tested with, but according to MSDN the onclickfires "when the user clicks the leftmouse button". I.e., by definition it doesn't occur for right (or middle) clicks. Given that's on MSDN you can expect IE to behave that way regardless of what the other browsers do.
不知道哪个浏览器(S),你已经与测试,但根据MSDN上的onclick火灾。“当用户点击左侧的鼠标按钮”。即,根据定义,它不会发生在右(或中间)点击中。鉴于这是在 MSDN 上,您可以期望 IE 以这种方式运行,而不管其他浏览器做什么。
(Onclick also fires for certain non-mouse things, like changing certain form elements with the keyboard, etc.)
(Onclick 也会触发某些非鼠标操作,例如使用键盘更改某些表单元素等。)
I know jQuery tries to normalise behaviour between browsers, but if the browser doesn't fire the event at all...
我知道 jQuery 试图规范浏览器之间的行为,但如果浏览器根本不触发事件......
There is at least one jQuery plugin that I know of that implements right-click: http://abeautifulsite.net/blog/2008/05/jquery-right-click-plugin/(I haven't used it, but it looks good except that it notes that Opera doesn't support it).
我知道至少有一个 jQuery 插件可以实现右键单击:http: //abeautifulsite.net/blog/2008/05/jquery-right-click-plugin/(我没用过,但看起来很好,只是它指出 Opera 不支持它)。