jQuery 将事件绑定到鼠标右键单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/706655/
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
Bind event to right mouse click
提问by Zac
How can I trigger some action with right click after disabling the browser context menu ?
禁用浏览器上下文菜单后,如何通过右键单击触发某些操作?
I tried this . . .
我试过这个。. .
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
$('.alert').fadeToggle();
return false;
});
});
回答by CMS
There is no built-in oncontextmenu event handler in jQuery, but you can do something like this:
jQuery 中没有内置的 oncontextmenu 事件处理程序,但您可以执行以下操作:
$(document).ready(function(){
document.oncontextmenu = function() {return false;};
$(document).mousedown(function(e){
if( e.button == 2 ) {
alert('Right mouse button!');
return false;
}
return true;
});
});
Basically I cancel the oncontextmenu event of the DOM element to disable the browser context menu, and then I capture the mousedown event with jQuery, and there you can know in the event argument which button has been pressed.
基本上,我取消 DOM 元素的 oncontextmenu 事件以禁用浏览器上下文菜单,然后我使用 jQuery 捕获 mousedown 事件,您可以在事件参数中知道按下了哪个按钮。
You can try the above example here.
你可以在这里尝试上面的例子。
回答by Bennett McElwee
The function returns too early.I've added a comment to the code below:
函数返回太早。我在下面的代码中添加了注释:
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
$('.alert').fadeToggle(); // this line never gets called
});
});
Try swapping the return false;
with the next line.
尝试将return false;
与下一行交换。
回答by Simon
Just use the event-handler. Something like this should work:
只需使用事件处理程序。这样的事情应该工作:
$('.js-my-element').bind('contextmenu', function(e) {
e.preventDefault();
alert('The eventhandler will make sure, that the contextmenu dosn't appear.');
});
回答by Güven Altunta?
I found this answer here and I'm using it like this.
我在这里找到了这个答案,我正在像这样使用它。
Code from my Library:
我的库中的代码:
$.fn.customContextMenu = function(callBack){
$(this).each(function(){
$(this).bind("contextmenu",function(e){
e.preventDefault();
callBack();
});
});
}
Code from my page's script:
来自我页面脚本的代码:
$("#newmagazine").customContextMenu(function(){
alert("some code");
});
回答by Stuart Bentley
document.oncontextmenu = function() {return false;}; //disable the browser context menu
$('selector-name')[0].oncontextmenu = function(){} //set jquery element context menu
回答by InforMedic
To disable right click context menu on all images of a page simply do this with following:
要在页面的所有图像上禁用右键单击上下文菜单,只需执行以下操作:
jQuery(document).ready(function(){
// Disable context menu on images by right clicking
for(i=0;i<document.images.length;i++) {
document.images[i].onmousedown = protect;
}
});
function protect (e) {
//alert('Right mouse button not allowed!');
this.oncontextmenu = function() {return false;};
}
回答by Arshid KV
.contextmenumethod :-
.contextmenu方法:-
Try as follows
尝试如下
<div id="wrap">Right click</div>
<script>
$('#wrap').contextmenu(function() {
alert("Right click");
});
</script>
.mousedownmethod:-
.mousedown方法:-
$('#wrap').mousedown(function(event) {
if(event.which == 3){
alert('Right Mouse button pressed.');
}
});
回答by BryanH
Is contextmenu
an event?
是contextmenu
活动吗?
I would use onmousedown
or onclick
then grab the MouseEvent
's buttonproperty to determine which button was pressed (0 = left, 1 = middle, 2 = right).
我将使用onmousedown
或onclick
然后获取MouseEvent
的按钮属性来确定按下了哪个按钮(0 = 左,1 = 中,2 = 右)。