js鼠標側鍵監聽(也有左鍵,中鍵和右鍵)


不嗶嗶,直接干貨。
js代碼:
document.onmousedown = function(event) {
                // var event = event || window.event//兼容ie低版本的
                if (event.button == 0) {
                    //鼠標左鍵 
                    alert("您點擊了鼠標左鍵!");
                } else if (event.button == 2) {
                    //鼠標右鍵 
                    alert("您點擊了鼠標右鍵!");
                } else if (event.button == 1) {
                    //鼠標中鍵 
                    alert("您點擊了鼠標中鍵!");
                } else if (event.button == 3) {
                    //鼠標側鍵3 
                    alert("您點擊了鼠標側鍵(后退)");
                } else if (event.button == 4) {
                    //鼠標側鍵4 
                    alert("您點擊了鼠標側鍵(前進)");
                }
            }

jQuery代碼(監聽的body,其實無所謂,主要是看evevt.button的值,用哪種代碼都一樣):

$("body").mousedown(function(event){ 
                        if(event.button == 0){ 
                          //鼠標左鍵 
                              alert("您點擊了鼠標左鍵!"); 
                        }else if(event.button == 2){ 
                          //鼠標右鍵 
                              alert("您點擊了鼠標右鍵!"); 
                        }else if(event.button == 1){ 
                          //鼠標中鍵 
                              alert("您點擊了鼠標中鍵!"); 
                        }else if(event.button == 3){ 
                          //鼠標側鍵3 
                              alert("您點擊了鼠標側鍵(后退)"); 
                        }else if(event.button == 4){ 
                          //鼠標側鍵4 
                              alert("您點擊了鼠標側鍵(前進)"); 
                        }
                  });

上班時間突發奇想,鼠標側鍵能監聽嗎?然后試着百度一下,結果只有左中右三鍵的,不過按照規律推測出來的側鍵,然后測試了一下,竟然可以,所以分享一下。我只試了兩款鼠標側鍵,如有問題可以聯系我。雖然我不一定能看見。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM