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

查看:108
本文介绍了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不会在
右键单击$ a
点击事件,虽然它触发
mousedown和mouseup。但是,
文档之间触发点击事件!由于 .live 在文档级别捕获
事件,所以
元素的click事件甚至
虽然元素本身不。如果
,您可以使用像mouseup这样的事件,
$ p 元素和文档
将看到该事件。

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中的一个bug,而不是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('click')点击右键单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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