在使用 Firefox* 时,将事件单击与实时功能结合使用会导致奇怪的行为。 在 Firefox 中,右键单击时也会触发单击! Internet Explorer 7 和 Google Chrome 中都不会发生同样的情况。
示例:
- Without live ,转到演示并尝试右键单击 的段落。对话框菜单应该 出现。
- With live ,去demo试试吧 点击“点击我!”。现在两个对话框 出现菜单和“另一段落”。
*使用 Firefox 3.5.3 进行测试
最佳答案
据我所知,这是一个已知问题(错误?)。您可以通过测试单击了哪个按钮来轻松解决此问题,如下所示:
$('a.foo').live("click", function(e) {
if (e.button == 0) { // 0 = left, 1 = middle, 2 = right
//left button was clicked
} else {
//other button was clicked (do nothing?)
//return false or e.preventDefault()
}
});
根据您的具体要求,您可能更喜欢使用switch
,但通常,如果单击左按钮以外的任何按钮,您可能只想什么也不做(或只是返回),如下所示上图:
$('a.foo').live("click", function(e) {
switch(e.button) {
case 0 : alert('Left button was clicked');break;
default: return false;
}
});
关于ajax - jQuery 单击事件的行为与 Firefox 中的实时功能不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1483907/