我需要知道在每个 mousemove 事件上按下哪个鼠标键,我尝试使用这个:
getMouseCode: function(e) { e = e || window.event; if (!e.which && e.button) { if (e.button & 1) e.which = 1; else if (e.button & 4) e.which = 2; else if (e.button & 2) e.which = 3; }; return e.which; },
但这仅适用于 chrome 和 IE7-8。 IE9 调试器总是说 e.button == 0 和 e.which == 1。经过一些调试,我发现 IE9 的 window.event 包含正确的值,所以我交换了
e = window.event || e;
这对 Safari 和 Air 也有效,但 Firefox 的 window.event 未定义,而 Opera 在回调参数和 window.event 对象中都有相同的错误值。
最佳答案
我在调查相关问题时正在考虑这个问题。事实证明,我的问题是我需要使用单独的函数来处理 onclick
和 onmouseover
事件。
我发现,当使用 Opera、Safari 和 FireFox 时,当没有单击鼠标按钮时,mousemove 事件对象的“which”属性被设置为 1。
关于javascript - mousemove 和跨浏览器 e.which,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10562950/