js实现浏览器右键菜单,屏蔽默认菜单

<div id="mouse" style="width:500px;height:200px;background:#E8FFE8;border:2px solid #336699;">
div>
<div id="menu" style="display:none; position: absolute; color:red; border-radius: 3px;background-color: #666;">
  <div style="padding:6px 6px 6px 6px;">
    <a href="http://www.baidu.com">撤回消息a> 
  div>
div>


<script>
function bindMouseEvent(el){
    var args = [].slice.call(arguments),
    el = el || document;
    args[0] = function(){},
    args[1] = args[1] || args[0],
    args[2] = args[2] || args[0],
    args[3] = args[3] || args[0],
    el.onmousedown = function(e){
        e = e || window.event;
        var button = e.button;
        if ( !e.which && isFinite(button) ) {
            e.which  = [0,1,3,0,2,0,0,0][button]; 
        }
        args[e.which](e);
    }
}
var el = document.getElementById("mouse");
var ex = document.getElementById("explanation");
var menu = document.getElementById("menu");
var left = function(){
    menu.style.display = 'none';
}
var middle = function(){
    menu.style.display = 'none';
}
var right = function(ev){
    var ev= ev || event;
    var scrollTop=document.documentElement.scrollTop || document.body.scrollTop;      
    menu.style.left = ev.clientX+scrollTop+'px';
    menu.style.top = ev.clientY+scrollTop+'px';
    menu.style.display = 'block';
}
bindMouseEvent(el,left,middle,right);
document.oncontextmenu=function(ev){
   return false;    //屏蔽右键菜单
}
script>

你可能感兴趣的:(js实现浏览器右键菜单,屏蔽默认菜单)