How to distinguish between left and right mouse click with jQuery

后端 未结 17 2680
独厮守ぢ
独厮守ぢ 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 23:01

    You can easily tell which mouse button was pressed by checking the which property of the event object on mouse events:

    /*
      1 = Left   mouse button
      2 = Centre mouse button
      3 = Right  mouse button
    */
    
    $([selector]).mousedown(function(e) {
        if (e.which === 3) {
            /* Right mouse button was clicked! */
        }
    });
    

提交回复
热议问题