接下来的代码记录窗口对象( 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>
适用于 Firefox 54.0.1
1. ------- CLICK ------
type: click
button: 2
detail: 1
1. ------- CONTEXTMENU ------
type: contextmenu
button: 2
detail: 1
适用于 Chrome 62.0.3165.0
1. ------- CONTEXTMENU ------
type: contextmenu
button: 2
detail: 0
我不知道 Firefox 出了什么问题,可能是浏览器或操作系统配置不正确。有同样的问题吗,请问如何解决?
最佳答案
我的 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
}
})
关于javascript - Firefox 与 contextmenu 事件同时触发 click 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45370448/