2013-03-25 118 views
3

我想用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://stackoverflow.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

回答

3

使用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'); 
    } 
}); 
相關問題