Firefox 会同时触发点击事件和上下文菜单事件



Next 代码在窗口对象 (FIDDLE( 上记录触发的事件:

var logEvent = (function() {
var count = 1,
timer = 0,
buffer = function() {
clearTimeout(timer)
timer = setTimeout(function() {
count++
}, 30)
}
return function(type, e) {
buffer();
console.log(count + '. ------- ' + type + ' ------')
console.log('type:   ' + e.type)
console.log('button: ' + e.button)
console.log('detail: ' + e.detail)
}
})()
window.addEventListener('click', function(e) {
logEvent('CLICK', e)
})
window.addEventListener('contextmenu', function(e) {
logEvent('CONTEXTMENU', e)
})
<body>
<div>
Click here
</div>
</body>
例如,如果我右键单击 body 元素,我将在控制台上获得下一次登录:

适用于火狐 54.0.1

1. ------- CLICK ------
type:   click
button: 2
detail: 1
1. ------- CONTEXTMENU ------
type:   contextmenu
button: 2
detail: 1

适用于铬 62.0.3165.0

1. ------- CONTEXTMENU ------
type:   contextmenu
button: 2
detail: 0

我不知道Firefox发生了什么,可能是浏览器或操作系统配置不正确。您是否遇到同样的问题,我该如何解决?

它也发生在我的火狐上。

这是一个已注册的错误,请参阅 https://bugzilla.mozilla.org/show_bug.cgi?id=184051

您可以通过在单击处理程序中检查 e.button 值来绕过它。

window.addEventListener('click', function(e) {
//when e.button==2, it's a right click, when 0, it's a left click
logEvent('CLICK.' + e.button, e);
if(e.button===2){
//do context menu stuff
}
})

最新更新