开发者

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

开发者 https://www.devze.com 2022-12-27 12:59 出处:网络
<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.

<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

精彩评论

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