button 事件属性

定义和用法

button 事件属性可返回一个整数,指示当事件被触发时哪个鼠标按键被点击。

语法

  1. event.button=0|1|2
参数 描述
0 规定鼠标左键。
1 规定鼠标中键。
2 规定鼠标右键。

Internet Explorer 拥有不同的参数:

参数 描述
1 规定鼠标左键。
4 规定鼠标中键。
2 规定鼠标右键。

提示和注释

注释:对于惯用左手的鼠标配置,上面的参数是颠倒的。

提示:Mozilla 的 Ctrl–Click 参数是 2 (等价于右击)。

实例

The following example alerts which mouse button was clicked:

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function whichButton(event)
  5. {
  6. if (event.button==2)
  7. {
  8. alert("You clicked the right mouse button!")
  9. }
  10. else
  11. {
  12. alert("You clicked the left mouse button!")
  13. }
  14. }
  15. </script>
  16. </head>
  17.  
  18. <body onmousedown="whichButton(event)">
  19.  
  20. <p>Click in the document. An alert box will
  21. alert which mouse button you clicked.</p>
  22.  
  23. </body>
  24. </html>