用jQuery处理鼠标左键和右键的点击操作

问题描述:

我想用jQuery链接点击链接。如果用鼠标左键单击链接,它工作正常。但是,如果使用了右键,它也应该可以工作。这里是代码:用jQuery处理鼠标左键和右键的点击操作

<a href="http://example.com" class="itemLink" data-count-url="/239/klicks"> 

$(".itemLink").on("click", function(event) { 
    $.ajax({ 
    type: 'POST', 
    url: $(event.target).attr("data-count-url") 
    }) 
}); 

据我所知click应该与左,右按钮一起工作。我的代码有什么问题?

+1

看到http://*.com/questions/1206203/how-to-distinguish-between-left-然后用鼠标右键点击jquery – gpasci 2013-03-25 08:53:07

+0

是的,使用mousedown而不是点击。 – kufudo 2013-03-25 08:53:46

使用mousedown事件,而不是click事件,并执行所有必要的行为

How to distinguish between left and right mouse click with jQuery

$('.itemLink').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'); 
    } 
});