How to distinguish between left and right mouse click with jQuery

后端 未结 17 2684
独厮守ぢ
独厮守ぢ 2020-11-21 22:36

How do you obtain the clicked mouse button using jQuery?

$(\'div\').bind(\'click\', function(){
    alert(\'clicked\');
});

this is trigger

17条回答
  •  礼貌的吻别
    2020-11-21 22:51

    To those who are wondering if they should or not use event.which in vanilla JS or Angular : It's now deprecated so prefer using event.buttons instead.

    Note : With this method and (mousedown) event:

    • left click press is associated to 1
    • right click press is associated to 2
    • scroll button press is associated with 4

    and (mouseup) event will NOT return the same numbers but 0 instead.

    Source : https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons

提交回复
热议问题