jQuery live(' click')触发右键单击 [英] jQuery live('click') firing for right-click

查看:61
本文介绍了jQuery live(' click')触发右键单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到jQuery中 live()函数的奇怪行为:

I've noticed a strange behaviour of the live() function in jQuery:

<a href="#" id="normal">normal</a>
<a href="#" id="live">live</a>

$('#normal').click(clickHandler);
$('#live').live('click', clickHandler);

function clickHandler() {
    alert("Clicked");
    return false;
}

这很好并且很花哨,直到您右键单击实时"链接并触发处理程序,然后不显示上下文菜单.事件处理程序根本不会(按预期)在正常"链接上触发.

That's fine and dandy until you right-click on the "live" link and it fires the handler, and then doesn't show the context menu. The event handler doesn't fire at all (as expected) on the "normal" link.

我已经能够通过将处理程序更改为此来解决此问题:

I've been able to work around it by changing the handler to this:

function clickHandler(e) {
    if (e.button != 0) return true;
    // normal handler code here
    return false;
}

但是将其添加到所有事件处理程序中确实很烦人.有没有更好的方法让事件处理程序像常规点击处理程序一样触发?

But that's really annoying to have to add that to all the event handlers. Is there any better way to have the event handlers only fire like regular click handlers?

推荐答案

这是已知问题:

似乎Firefox不会触发元素上的click事件右键单击,尽管它会触发mousedown和mouseup.但是,确实 document 上触发点击事件!由于 .live 可以捕获它可以看到文档级别的事件元素的click事件尽管元素本身没有.如果您使用像mouseup这样的事件 p 元素和 document 将会看到事件.

It seems like Firefox does not fire a click event for the element on a right-click, although it fires a mousedown and mouseup. However, it does fire a click event on document! Since .live catches events at the document level, it sees the click event for the element even though the element itself does not. If you use an event like mouseup, both the p element and the document will see the event.

您的变通办法是您目前最好的解决方法.它似乎只影响Firefox(我相信这实际上是Firefox中的错误,而不是jQuery本身).

Your workaround is the best you can do for now. It appears to only affect Firefox (I believe it's actually a bug in Firefox, not jQuery per se).

另请参见此问题昨天.

这篇关于jQuery live(&amp;#39; click&amp;#39;)触发右键单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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