使用 jQuery 检测中间按钮单击(滚动按钮) [英] Detect middle button click (scroll button) with jQuery

查看:31
本文介绍了使用 jQuery 检测中间按钮单击(滚动按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标签的列表,可以在点击时播放一些 mp3 文件.使用 jQuery 绑定click"事件时它工作正常:

I have a list with a tags to play some mp3 files onclick. It is working fine when binding on the 'click' event with jQuery:

$oo.data({'__mp3play':true,'wapiHandle':h+0,'wapiIndex':o.ajaxPlayList[h].length})
           .bind( 'click', function()
            { var wh = $j(this).data('wapiHandle');
              if( typeof o.regObjects[wh] == 'object' && o.regObjects[wh].play(this.href))
               { return false; }
            });

点击鼠标左键时:它在加载我的 Flash 插件时禁用默认处理,否则它将正常打开.

When clicking the left mouse button: It disables default handling when my flash plugin is loaded otherwise it will be opened normally.

但是:当我使用鼠标的滚动按钮并单击它时,不会触发单击事件并且链接正常打开.

BUT: When I use the scrollbutton of the mouse and click on it, the click event will not fired and the link opens normally.

我尝试使用 mousedown 或 mouseup 事件,但没有帮助,链接总是正常打开,副作用是音乐也开始用 Flash 播放器播放.

I have tried to use mousedown or mouseup events but doesn't help, the link always opens normally with the side effect that the music starts playing also with the flash player.

还有 preventDefault() 根本不起作用.

Also preventDefault() does not work at all.

谁能告诉我如何检测鼠标中键点击(滚动按钮点击)?

Can somebody tell me how to detect the middle mouse button click (scroll button click)?

感谢您的评论.

PS:我已经尝试过有关此站点上可用的中间按钮"的其他解决方案.

PS: I have already tried other solutions about the 'middle button' available on this site.

在各种浏览器中测试,结果相同.

Tested in all kind of browsers with the same result.

这也不起作用,使用鼠标中键时链接将正常打开.使用鼠标左键时,没有任何反应.

This also does not work, the link will be opened normally when using the middle mouse button. When using the left mouse button, nothing happen.

$oo.bind( 'mousedown click mouseup', function(e)
{ e.preventDefault(); e.stopPropagation(); return false; });

推荐答案

好的,

感谢您的意见.'@no.good.at.coding' 有一个很好的解决方案,但不适用于 IE(见下文),但它是一个很好的起点.我把他的代码改成这样:

Thank you for your input. '@no.good.at.coding' has a nice solution but does not work on IE (see later on this writing) but is a good starting point. I change his code to this:

// Avoid relations will be opened in extra tab when clicking middle-scroll-button to open it
$(document).bind($.browser.msie ? "mousedown" : "click", function(e)
{
    if (e.which == 2 && e.target.tagName == 'A') 
    {
        var bIE = $.browser.msie,
            $o = $(e.target),
            oe = $o.data('events'),
            b = true,
            f = function(){};

        if (typeof oe == 'object' && oe['click']) {
            f = function(){ $o.trigger('click'); }
        } else {
            b = (typeof $o[0].href == 'string' && ($o[0].target == undefined || $o[0].target == '' || $o[0].target == '_self'));
            if (b) { 
                f = function () { window.location.href=$o[0].href; };
            }
        }

        if (!b) { return; }

        e.stopImmediatePropagation();
        e.stopPropagation();
        e.preventDefault();

        if (bIE)
        {
            if (!window.__swcp__) { 
                window.__swcp__= $('<div style="position:fixed;top:0px;left:0px;width:100%;height:100%;background:#000;display:block;z-index:9999;opacity:0.01;filter:alpha(opacity:1);" />').appendTo('body'); 
            }
            window.__swcp__.show();
        }

        setTimeout(f, 50);
        if (bIE) { 
            setTimeout( function() { window.__swcp__.hide(); }, 1000 );
        }

        return false;
    }
});

中键现在就像鼠标的左键一样.正如你所看到的 IE 需要更多,我们必须将链接标签移开 1 秒钟,以避免它会在额外的选项卡中打开.我在这里使用一种简单的方法来做到这一点,创建一个为眼睛"透明 div(不透明度 = 0.01),将放置在窗口内容上,因此页面上的任何链接都将不聚焦.用户不会注意到任何区别,只是鼠标指针可以从指针变为默认值,一秒后又变回指针(仅限 IE).我可以忍受这一点.

The middle-button acts now like the left-button of a mouse. As you can see IE needs some more, we must unfocus the link tag for 1 second to avoid that it will be opened in an extra tab. I use here a simple way to do this, create a 'for the eye" transparent div (opacity = 0.01) that will be placed over the window content so any link on the page will be unfocused. The user will not notice any difference except that the mouse pointer can change from pointer to default and after one second it change back to pointer (IE only). I can live with that.

我接受这个答案,但如果你有更好的想法,请告诉我.

I accept this as an answer, but if you have better idea's let me know.

这篇关于使用 jQuery 检测中间按钮单击(滚动按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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