录制onmousedown
和onmouseup
以在onmousemove
中使用不起作用,因为onmouseup
仅在按钮时触发一次发布到窗外:http://jsfiddle.net/f1nqproy/5/
event.button
仅在 Internet Explorer 中返回有意义的结果。
event.buttons
仅存在于 Firefox 中。
那么使用其他浏览器怎么办?
编辑:
MouseEvent.buttons 现在已经标准化,所以这个问题已经解决了:
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
最佳答案
如果你想做一些与浏览器无关的事情,最简单的方法是使用 jQuery
jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.
您可以轻松绑定(bind)到mouseup , mousedown事件并忘记浏览器兼容性
$( window).mouseup(function(e){
console.log("mouseup:", e.button);
});
$( window).mousedown(function(e){
console.log("mousedown:", e.button);
});
-----更新------
IE(像往常一样)做了另一项工作,因此如果指针位于窗口之外,您将不会收到 mouseup 事件。
一个技巧是使用 mouseleave 事件:
//hack for IE
$( window).mouseleave (function(e){
console.log("mouseleave");
});
关于javascript - 无法检测到鼠标移动时按下的鼠标按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162440/