JS special mouse buttons

帅比萌擦擦* 提交于 2019-12-07 10:01:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!