开发者

jQuery click() event; how to prevent this event on right mouse btn click?

<script>
 $("#test_img").click(function() {
   alert("Hello");
 });
</script>

But it doesn't matter what mouse button I'm pressing - right or left. I see absolutely the same result. I want alert to be shown only on left mouse button click.

Thank you and sorry; I'm little new to javascript.

开发者_C百科

Thank you.


You can evaluate the event.which attribute to determine which button has been pressed.

$("#test_img").mouseup(function(e) { 
  // e.which is 1, 2 or 3 for left / middle / right mouse button
  if (e.which === 1) {
    //continue
  }
}); 

Furthermore, to safely detect a mousebutton, you cannot use the click-event! Use mousedown or mouseup instead.

Try it out here!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜