问题
My mouse has two buttons on the side, whose default behaviour are "Back" and "Forward".
What I'd like to know is whether or not it's possible to detect clicks of these mouse buttons in JavaScript, or if these are "special" buttons akin to the keyboard's "Play", "Volume Up" and "Wireless on/off" buttons.
回答1:
I am not aware of any specific mouse events.
You can, however, easily find out yourself by inspecting the event object of the mousedown event. Fullscreen fiddle: 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] = "";
}
来源:https://stackoverflow.com/questions/7906619/js-special-mouse-buttons