检测到鼠标左键按下 [英] Detect left mouse button press
问题描述
我使用这段代码
//如果evt载入,则返回true鼠标左键按
函数detectLeftButton(evt){
// W3C
if(window.event == null){
return(evt.button == 0)
}
// IE
else {
return(evt.button == 1);
}
}
然而,它在Opera和Chrome中不起作用,因为它恰好在那里也存在。
那么我该怎么做?我有一些浏览器检测,但我们都知道它不能依赖一些浏览器最近做的所有屏蔽。如何检测鼠标左键RELIABLY?
更新后的答案。以下内容将检测是否只按下鼠标左键:
function detectLeftButton(evt){
evt = evt || window.event;
if(evt中的按钮){
return evt.buttons == 1;
}
var button = evt.which || evt.button;
返回按钮== 1;
$ / code>
有关在JavaScript中处理鼠标事件的更多信息,请尝试 http://unixpapa.com/js/mouse.html
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?
Updated answer. The following will detect if the left and only the left mouse button is pressed:
function detectLeftButton(evt) {
evt = evt || window.event;
if ("buttons" in evt) {
return evt.buttons == 1;
}
var button = evt.which || evt.button;
return button == 1;
}
For much more information about handling mouse events in JavaScript, try http://unixpapa.com/js/mouse.html
这篇关于检测到鼠标左键按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!