Firefox 与 contextmenu 事件同时触发 click 事件

2024-01-02

接下来的代码记录窗口对象上触发的事件(FIDDLE https://jsfiddle.net/Makha92/kh8roneh/1/):

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>
If I right click on the body element for example, I'll get next log on console:

适用于火狐 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 出了什么问题,可能是浏览器或操作系统配置不正确。您是否也遇到了同样的问题,我该如何解决?


我的火狐也出现这种情况。

这是一个已注册的错误,请参阅https://bugzilla.mozilla.org/show_bug.cgi?id=184051 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
    }
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Firefox 与 contextmenu 事件同时触发 click 事件 的相关文章

随机推荐