Jquery:检测是否点击中间或右键按钮,如果是,执行: [英] Jquery: detect if middle or right mouse button is clicked, if so, do this:

查看:197
本文介绍了Jquery:检测是否点击中间或右键按钮,如果是,执行:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看我的jsfiddle 演示,如果 e.which == 1 然后当你离开点击h2它将
e.which == 2 e.which == 3 然后它不工作。 2是鼠标中键,3是鼠标右键。我也发现了这一点:

Check out my jsfiddle demo, if e.which == 1 then when you left click the h2 it will e.which == 2 or e.which == 3 then it wont work. 2 is the middle mouse button, and 3 is the right mouse button. i found this too:

JQuery提供了一个e.which属性,分别为左,中,右键分别返回1,2,3。所以你也可以使用if(e.which == 3){alert(right click); }

JQuery provides an e.which attribute, returning 1, 2, 3 for left, middle, and right click respectively. So you could also use if (e.which == 3) { alert("right click"); }

此代码无效:

代码: >

code:

    $("h2").live('click', function(e) { 
   if( e.which == 2 ) {
      e.preventDefault();
      alert("middle button"); 
   }
});


推荐答案

您可能想要陷阱mousedown事件,还需要防止oncontextmenu事件在右键单击事件期间停止上下文菜单。

You may want to trap the mousedown event, and you also need to prevent the oncontextmenu event to stop the context menu from coming up during the right click event.

$("h2").live('mousedown', function(e) { 
   if( (e.which == 1) ) {
     alert("left button");
   }if( (e.which == 3) ) {
     alert("right button");
   }else if( (e.which == 2) ) {
      alert("middle button"); 
   }
   e.preventDefault();
}).live('contextmenu', function(e){
   e.preventDefault();
});

这篇关于Jquery:检测是否点击中间或右键按钮,如果是,执行:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆