鼠标右键单击Firefox会触发单击事件 [英] Mouse right click on Firefox triggers click event

查看:124
本文介绍了鼠标右键单击Firefox会触发单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在Firefox上单击鼠标右键会触发一个addEventListener.

I noticed that the mouse right click on Firefox triggers an addEventListener.

我在更多浏览器和更多操作系统(IE 11-10-9,Safari,Chrome)上尝试了此代码,并通过右键单击鼠标,仅在Firefox上始终打印console.log消息.

I tried this code on more browsers and more OS (IE 11-10-9, Safari, Chrome) and by pressing the mouse right click, only on Firefox the console.log message is always printed.

<div id="one-div" style="height:400px;width:500px;background-color:#000;"> click me </div>
<script>
    function cb(event, from){
        // if click is fired on <div> with:
        // left click, both EventListener will be printed.
        // right click, only the 'document' one will be printed.
        event.preventDefault();
        console.log(event + ' from: ' + from );
    }
    document.addEventListener('click', function(e){
        cb(e,'document');
    }, false);
    document.getElementById("one-div").addEventListener('click', function(e){
        cb(e,'one-div');
    }, false);
</script>

我还注意到,当将点击触发到div中时,它仅触发document.addEventListener. 我搜索了Firefox changelog,但没有相关新闻.

And also I noticed that, when the click is fired into the div, it triggers the document.addEventListener only. I searched on Firefox changelog but no news about this.

任何人都可以解释这种行为吗? 谢谢!

Can anyone explain this behavior? Thanks!

推荐答案

默认情况下,在所有浏览器中, addEventListener('contextmenu'),否则右键单击将打开一个包含一些选项的窗口(每个浏览器都有不同的选择).

By default, in all browsers right click event is captured by addEventListener('contextmenu'), otherwise a right click will open a window with some options (each browser has different one).

在Firefox中,将addEventListener('click')添加到document对象时,它将捕获文档上的任何鼠标单击事件(左,右,滚轮),并且将禁用

In Firefox, when you add addEventListener('click') to the document object, it will capture any mouse clicking events (left, right, wheel) on the document and it will disable this right click behavior.

此外,这就是 Mozilla文档鼠标事件部分,尽管直到您将侦听器添加到document对象

In addition, this is what Mozilla documentation says in Mouse Events section, although the (ANY button) stuff is not activated until you add the listener to the document object

点击:在元素上按下并释放了指向设备按钮(ANY按钮;不久将成为主要按钮).

click: A pointing device button (ANY button; soon to be primary button only) has been pressed and released on an element.

*注意::当您双击鼠标右键时,仍会显示上面的窗口,而单击时不会显示.

*Note: still the above window is shown when you double click right mouse button, but not with a single click.

这篇关于鼠标右键单击Firefox会触发单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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