--- title: event.button slug: conflicting/Web/API/MouseEvent/button translation_of: Web/API/MouseEvent/button translation_of_original: Web/API/event.button original_slug: Web/API/event.button ---
{{ ApiRef() }}
表明当前事件是由鼠标的哪个按键触发的.
event.button
var buttonCode = event.button;
该属性返回一个整数值,代表触发当前事件的鼠标按键.在通常情况下,对应关系如下:
The order of buttons may be different depending on how the pointing device has been configured.
<script> var whichButton = function (e) { // 处理不同的事件模型 var e = e || window.event; var btnCode; if ('object' === typeof e) { btnCode = e.button; switch (btnCode) { case 0: alert('你按了左键.'); break; case 1: alert('你按了中键.'); break; case 2: alert('你按了右键.'); break; default: alert('未知按键: ' + btnCode); } } } </script> <button onmouseup="whichButton(event);" oncontextmenu="event.preventDefault();">请点击鼠标...</button>
Because mouse clicks are frequently intercepted by the user interface, it may be difficult to detect buttons other than those for a standard mouse click (usually the left button) in some circumstances.
Users may change the configuration of buttons on their pointing device so that if an event's button property is zero, it may not have been caused by the button that is physically left–most on the pointing device; however, it should behave as if the left button was clicked in the standard button layout.
DOM 2 Events Specification: button
Based on Jan Wolter's JavaScript Madness: Mouse Events.
{{ CompatibilityTable() }}
Feature | Gecko | Webkit | Internet Explorer | Opera |
---|---|---|---|---|
Basic support | 1 | 523 | 9 | 8 |
{{ languages( { "ja": "ja/DOM/event.button", "pl": "pl/DOM/event.button" ,"en": "en/DOM/event.button" } ) }}