JS特殊鼠标按钮

5

我的鼠标侧面有两个按钮,默认行为是“后退”和“前进”。

我想知道的是是否可以在JavaScript中检测这些鼠标按钮的单击事件,或者它们是否类似于键盘上的“播放”,“音量增加”和“无线开关”按钮等“特殊”按钮。


你可以尝试事件是否被触发:http://jsfiddle.net/pimvdb/2c8gs/2/。你能聚焦结果的iframe并按下那些鼠标按钮吗? - pimvdb
1个回答

3

我不知道具体的鼠标事件。

但是,你可以通过检查mousedown事件的event对象来轻松找到。

全屏演示:http://jsfiddle.net/dWfDL/1/show/
var text = typeof document.body.textContent != "undefined" ? "textContent" : "innerText";
window.onmousedown = function(e){
    //Inspect the `e` object, for example using a for(var i in e) loop, or:
    //console.log(e);
    var s = [];
    for(var i in e){
        try{
            if(e[i] !== null){
                if(i.toUpperCase() == i) continue; //Ignore constants
                if(typeof e[i] == "function") continue; //Ignore functions
                if(e[i].parentNode) continue; //Ignore elements
                if(e[i] === window) continue; //Ignore Window
            }
            s.push(i + " =\t\t" + e[i]);
        }catch(err){s.push(i + " \tERROR reading property.")}
    }
    e.preventDefault();
    s = s.join("\n") + "\n\n";
    document.body[text] = s + document.body[text];
}
//Double-click to wipe contents
window.ondblclick = function(){
    document.body[text] = "";
}

1
你可能需要添加 return falsee.preventDefault(),因为鼠标似乎告诉浏览器要切换页面。如果浏览器没有拦截,那么你就知道它不可用了。 - Dominic Barnes
@DominicBarnes 不错的观点。我会在下一个答案更新时包含它(我正在为此创建一个fiddle)。 - Rob W
好的,所以我的问题的答案是“不”,但这帮助我意识到event.which在所有主要浏览器中都得到支持(除了IE 8及以下版本),这让我修复了代码中其他地方的一些问题。谢谢! - Niet the Dark Absol

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接