Javascript 判断鼠标点击右击事件

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6     <script type="text/javascript">
 7        window.onload = function(){
 8            //去掉默认的contextmenu事件,否则会和右键事件同时出现。
 9            document.oncontextmenu = function(e){
10                e.preventDefault();
11            };
12            document.getElementsByTagName("body")[0].onmousedown = function(e){
13                if(e.button ==2){
14                    alert("你点了右键");
15                }else if(e.button ==0){
16                    alert("你点了左键");
17                }else if(e.button ==1){
18                    alert("你点了滚轮");
19                }
20            }
21        }
22     </script>
23 </head>
24 <body>
25  
26 <div style="width: 400px;height:400px;margin:auto;border:1px solid pink" id="test"></div>
27 </body>
28 </html>

 

posted @ 2017-08-21 14:07  wǒの湁茡潞釦﹌  阅读(396)  评论(0编辑  收藏  举报