你如何获得使用jQuery点击鼠标按钮?
$('div').bind('click', function(){
alert('clicked');
});
这是由右键和左键触发,能够捕捉鼠标右键单击的方式是什么?如果像下面这样的东西存在,我会很高兴:
$('div').bind('rightclick', function(){
alert('right mouse button is pressed');
});
我改变了它的工作动态添加元素.on()在jQuery 1.7或以上使用:
$(document).on("contextmenu", ".element", function(e){
alert('Context Menu event has fired!');
return false;
});
这是我的:
$('.element').bind("contextmenu",function(e){
alert('Context Menu event has fired!');
return false;
});
如果你是多种解决方案^^
从jQuery版本1.1.3开始,event.which规范化event.keyCode,event.charCode所以你不必担心浏览器的兼容性问题。关于文件event.which
event.which 将分别给左,中,右鼠标按钮1,2或3,这样:
$('#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!');
}
});