检测鼠标左键按下
我讨厌 W3C 和 MS 创建的鼠标按钮的混乱!我想知道当我收到 mousedown 事件时是否按下了鼠标左键。
我使用这段代码
// Return true if evt carries left mouse button press
function detectLeftButton(evt) {
// W3C
if (window.event == null) {
return (evt.button == 0)
}
// IE
else {
return (evt.button == 1);
}
}
但是,它在 Opera 和 Chrome 中不起作用,因为碰巧 window.event 也存在在那里。
那我该怎么办?我有一些浏览器检测,但我们都知道它不能依赖于一些浏览器最近所做的所有屏蔽。如何可靠地检测鼠标左键?
I hate this mess with the mouse buttons created by W3C an MS! I want to know if the left mouse button is pressed when I get a mousedown event.
I use this code
// Return true if evt carries left mouse button press
function detectLeftButton(evt) {
// W3C
if (window.event == null) {
return (evt.button == 0)
}
// IE
else {
return (evt.button == 1);
}
}
However, it does not work in Opera and Chrome, because it so happens that window.event exists there too.
So what do I do? I have some browser detection, but we all know it cannot be relied upon with all the masking some browsers do lately. How do I detect the left mouse button RELIABLY?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新了答案。以下将检测是否按下鼠标左键且仅按下鼠标左键:
有关在 JavaScript 中处理鼠标事件的更多信息,请尝试 http://unixpapa.com/js/mouse.html
Updated answer. The following will detect if the left and only the left mouse button is pressed:
For much more information about handling mouse events in JavaScript, try http://unixpapa.com/js/mouse.html
现在有一个 W3C 标准 < 标准模式下的 IE9 和 Gecko 15+< /a>.
W3C 完全塞满了 event.button 属性,因此对于符合标准的browser event.button 为 0,但对于标准之前创建的浏览器,event.button 为 1。
因此代码必须避免使用
event.button
(旧版浏览器除外)。以下代码应该可以工作:There is now a W3C standard
event.buttons
property supported by IE9 in standards mode, and Gecko 15+.The W3C completely stuffed up the event.button property, so for a standards compliant browser event.button is 0, but for browsers created before the standard, event.button is 1.
So code must avoid using
event.button
except for older browsers. The following code should work:您可以使用以下代码 -
You can use the following code-
尽管 event.buttons 现在可以在 Chrome 中使用,但 Safari 仍然不支持它。一种解决方法是在文档或父级别使用 onmousedown 和 onmouseup 事件,例如:
onmousedown="bMouseDown=true" onmouseup="bMouseDown=false"
Even though event.buttons now works in Chrome, Safari still does not support it. One workaround is to use onmousedown and onmouseup events at document or parent level like:
onmousedown="bMouseDown=true" onmouseup="bMouseDown=false"
说明:当鼠标按下时,我们将按钮数组中按下的按钮更改为 true。
当鼠标抬起时,我们将按下的按钮更改为 false。
现在我们可以更准确地确定哪个按钮被按下,但是我们遇到了右键单击问题..因为使用该按钮我们在浏览器中打开一个上下文菜单,并且逃脱了我们的控制...所以,我们禁用上下文菜单以便正确检测右键。如果我们不这样做,我们也必须解决左键单击问题……这是一个逃避此响应的复杂情况。
为了简化事情,我们可以添加另一个变量
mousePressed
并标记鼠标是否按下或向上。在 Chrome 上完美运行,我没有在其他浏览器中测试它,但我想它在 Firefox 和 Opera 中也可以...... IE ???我不在乎IE。
Explanation: When mouse is down, we change to true the pressed button in our buttons array.
When mouse is up, we change to false the pressed button to false.
Now we can establish which button is pressed more accurately, but we have a right click problem.. because with that button we open a contextmenu at the browser, and that escapes our control... so, we disable the context menu in order to properly detect right click. If we don't do that, we must resolve left click too... and its a complication that escapes this response.
In order to simplify things, we can add another variable
mousePressed
and flag if mouse is down or up.Works perfect on chrome, I didn't test it in other browser but I guess its ok in firefox and opera too... IE??? I don't care IE.