鼠标右键事件

1:只是需要console.log这种与右键菜单不冲突的事件时,直接使用oncontextmenu完成

2:一是对重写的下拉菜单的隐藏和显示;二是屏蔽默认的鼠标右键事件;三是鼠标左键点击页面下拉菜单隐藏

3:当contextmenu无法使用的时候,用这个

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript">
       window.onload = function(){
           //去掉默认的contextmenu事件,否则会和右键事件同时出现。
           document.oncontextmenu = function(e){
               e.preventDefault();
           };
           document.getElementById("test").onmousedown = function(e){
               if(e.button ==2){
                   alert("你点了右键");
               }else if(e.button ==0){
                   alert("你点了左键");
               }else if(e.button ==1){
                   alert("你点了滚轮");
               }
           }
       }
    </script>
</head>
<body>
 
<div style=" 400px;height:400px;margin:auto;border:1px solid pink" id="test"></div>
</body>
</html>
原文地址:https://www.cnblogs.com/cndotabestdota/p/7230289.html