首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javascript判断鼠标上下键-兼容firefox、ie、chrome

2012-07-27 
javascript判断鼠标左右键-兼容firefox、ie、chrome用javascript判断鼠标点击下去左右键的方法很简单,给对象

javascript判断鼠标左右键-兼容firefox、ie、chrome

用javascript判断鼠标点击下去左右键的方法很简单,给对象加上onmousedown事件,通过javascript事件的e.button值来判断。

?

例子如下:

document.onmousedown = testmouse;function testmouse(e){var e = window.event || e;var value = e.button;if(value == 2 || value ==3)       {     alert("右键");               }              else              {                alert("左键");              }}

var e = window.event || e;

这句用来得到事件对象,兼容了各种浏览器。e.buttton的值,有的浏览器左键值为0,有的左键值为1。同样,有的右键的值为2,有的值为3。

热点排行