检测鼠标中键点击事件jQuery

当用户点击鼠标左键或中间的鼠标按钮时,我想将jQuery-UI对话框显示为弹出窗口。 它适用于左键单击(我得到警告框,然后弹出窗口),但不适用于中间(既没有警报框也没有弹出窗口)。 我错过了什么?

$('a.external').live('click', function(e){ if( e.which <= 2 ) { e.preventDefault(); alert ("inside if"); } popUp.start(this); }); 

使用mousedownmouseup而不是click 。 并且(除非您使用的是非常旧版本的jQuery)使用.on()而不是.live()

 $(document).on("mousedown", "a.external", function(e) { if( e.which <= 2 ) { e.preventDefault(); alert ("inside if"); } popUp.start(this); }); 

...理想情况下,您使用的父元素比document更接近链接。

演示: http : //jsfiddle.net/7S2SQ/

为了在Firefox(40.0.3)中完全运行,我必须实现.on('mouseup', fn) ,如下所示:

 $(selector).on('mouseup', function (e) { switch (e.which) { // Left Click. case 1: // By way of example, I've added Ctrl+Click Modifiers. if (e.ctrlKey) { // Ctrl+LeftClick behaviour. } else { // Standard LeftClick behaviour. } break; // Middle click. case 2: // Interrupts "Firefox Open New Tab" behaviour. break; // Default behaviour for right click. case 3: return; } // Pass control back to default handler. return true; });