Firefox与contextmenu事件同时触发click事件 [英] Firefox fires click event at the same time with contextmenu event

查看:163
本文介绍了Firefox与contextmenu事件同时触发click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接下来的代码将触发事件记录在窗口对象上( FIDDLE ):

Next code logs the fired events on window object (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元素,则将在控制台上获得下一次登录:

If I right click on the body element for example, I'll get next log on console:

对于Firefox 54.0.1

for 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

for Chrome 62.0.3165.0

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

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

I don't know what's going on with Firefox, maybe browsers or operational systems configuration configured improperly. Did you have same problems, how can I fix it?

推荐答案

它也在我的firefox上发生.

It happens on my firefox too.

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

It's a registered bug, see https://bugzilla.mozilla.org/show_bug.cgi?id=184051

您可以通过检查点击处理程序中的e.button值来解决此问题.

You can go around it by checking e.button value in the click handler.

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
    }
})

这篇关于Firefox与contextmenu事件同时触发click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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