这篇翻译不完整。请帮忙从英语翻译这篇文章

MouseEvent.button是只读属性,它返回一个值,代表用户按下并触发了事件的鼠标按键。

这个属性只能够表明在触发事件的单个或多个按键按下或释放过程中哪些按键被按下了。因此,它对判断mouseentermouseleavemouseovermouseout or mousemove这些事件并不可靠。

用户可能会改变鼠标按键的配置,因此当一个事件的MouseEvent.button值为0时,它可能不是由物理上设备最左边的按键触发的。但是对于标准按键布局的鼠标设备来说,这个值应该是能正确使用的。

注意:MouseEvent.buttons 属性可指示任意鼠标事件中鼠标的按键情况,因此不要把它和MouseEvent.button属性弄混淆了。

语法

var buttonPressed = instanceOfMouseEvent.button

返回值

一个数值,代表按下的鼠标按键:

  • 0:主按键被按下,通常指鼠标左键 or the un-initialized state
  • 1:辅助按键被按下,通常指鼠标滚轮 or the middle button (if present)
  • 2:次按键被按下,通常指鼠标右键
  • 3:第四个按钮被按下,通常指浏览器后退按钮
  • 4:第五个按钮被按下,通常指浏览器的前进按钮

对于配置为左手使用的鼠标,按键操作将正好相反。此种情况下,从右至左读取值。

例子

<script>
var whichButton = function (e) {
    // Handle different event models
    var e = e || window.event;
    var btnCode;

    if ('object' === typeof e) {
        btnCode = e.button;

        switch (btnCode) {
            case 0:
                console.log('Left button clicked.');
            break;

            case 1:
                console.log('Middle button clicked.');
            break;

            case 2:
                console.log('Right button clicked.');
            break;

            default:
                console.log('Unexpected code: ' + btnCode);
        }
    }
}
</script>

<button onmouseup="whichButton(event);" oncontextmenu="event.preventDefault();">Click with mouse...</button>

Specifications

Specification Status Comment
Document Object Model (DOM) Level 3 Events Specification
MouseEvent.button
Obsolete Compared to Document Object Model (DOM) Level 2 Events Specification, the return value can be negative.
Document Object Model (DOM) Level 2 Events Specification
MouseEvent.button
Obsolete Initial definition.

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Edge Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support (Yes) 1.0 (1.7 or earlier) 1.0 9.0 [1] (Yes) 3.0.4
Feature Edge Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) ? ? ? ? ?

[1] This convention is not followed in Internet Explorer prior to version 9: see QuirksMode for details.

See also

文档标签和贡献者

此页面的贡献者: QZhongyi, franose371, Dcfm
最后编辑者: QZhongyi,