将 IE event.button 转换为 W3C event.button
在按下鼠标(和向上)时,我需要检查哪个鼠标按钮已更改其状态。这对于 W3C 模型甚至旧的 netscape 方式(event.which)来说都很容易,但 IE 让我头疼。我知道 IE 中的 event.button 是一个位掩码,但问题是我无法从该位掩码中找出按下了哪个按钮。如果 IE 给我 event.button == 3 那么我无法判断用户是否在右侧按钮已按下时按下了左侧按钮,或者用户是否在左侧按钮已按下时按下了右侧按钮。
为了一劳永逸地解决这个问题,我正在寻找一个很好的通用解决方案,将损坏的 IE 事件转换为 W3C 兼容事件。也许有人已经这样做了并想分享?
On mouse-down (and up) I need to check which mouse button has changed its state. This is pretty easy with the W3C model and even the old netscape way (event.which) but IE gives me a headache. I know that event.button in IE is a bitmask but the problem is that I can't find out which button was pressed from this bitmask. If IE gives me event.button == 3 then I can't tell if the user pressed the left button while the right button was already down or if the user pressed the right button while the left button was already down.
To solve this problem once and for all I'm searching for a nice generic solution to convert the broken IE event into a W3C compatible event. Maybe someone already did that and wants to share?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IMO(以及 quirksmode 的),IE 的位屏蔽比 W3C 版本更好,因为可以检测组合点击,例如左+右或中+左。它提供了更多的灵活性。两者(按下的按钮总数和按下的最新按钮)都会更好。
我不知道有哪个库或脚本已经完成了您想要做的事情,我认为大多数开发人员都会忽略组合点击,除非他们想对组合点击应用特定操作。另外,这相当棘手,您必须检测整个文档的
onmousedown
和onmouseup
并记录鼠标按钮的状态。然后,在您想要施展魔法的元素上,从新的鼠标状态中减去之前的鼠标状态。类似的东西(经过测试):当然,这个脚本只有大约一百万个可能出错的地方。例如,在我的示例中,我有一个弹出的警报窗口,其中包含真实的按钮状态。如果在警报对话框出现时释放按钮,文档将不会检测到鼠标松开,并且会使一切变得不正常。同样,其他元素上的任何 mouseup 或 mousedown 事件停止传播到 documentElement 的事件处理程序都会导致它中断。
如果您需要我的建议,请在组合鼠标单击时显示一条消息,猜测“正确”的鼠标按钮或忽略它们。这些选项中的任何一个都比黑客方法更好。
IMO (and quirksmode's), IE's bitmasking is better than the W3C version because you can detect combo clicks, like left+right or middle+left. It offers a bit more flexibility. Both (total buttons pressed and newest button pressed) would be even better.
I don't know of a library or script that already does what you're trying to do, I think most devs would just ignore combo clicks unless they wanted to apply a specific action to a combo click. Also, it's rather tricky, you'd have to detect both
onmousedown
andonmouseup
for the entire document and record the state of the mouse buttons. Then on the element you want to work your magic on, subtract the previous mouse state from the new one. Something like (tested):Of course, there are only about a million things that could go wrong with this script. For instance, in my example there I have an alert window that pops up with the real button state. If you release the button whilst that alert dialog is up, the document won't detect the mouseup and it will throw everything out of whack. Likewise, any mouseup or mousedown events on other elements that stop propagation up to the documentElement's event handler would cause it to break.
If you want my advice, display a message on a combo mouse click, guess at the "correct" mouse button or ignore them all together. Any one of those options is better than a hacky approach.
您可能会发现此页面很有用:http://unixpapa.com/js/mouse.html 。它有一个关于鼠标按钮检测的部分,我以前使用过并且发现它有用且准确,包括以下内容:
You may find this page useful: http://unixpapa.com/js/mouse.html. It has a section on mouse button detection that I have used before and found useful and accurate, including the following: