右键弹出菜单的实现

 

 

 

<html>
<head>
 
<link href="menu.css" rel="stylesheet" type="text/css" />
	
</head>
<body  id="body">
 
  	
<div id="custom_menu" class="custom_menu">
	<div><a href="#">班级管理</a></div>
	<div><a href="#">人员管理</a></div>
	<div><a href="#">报名管理</a></div>
	<input type="button" value="关闭" onclick="closeMenu(this)">
</div>	
  	
  	
  	<div id="useMenu" style="border:solid 1px red;padding: 2px">
    	<a href="javascript:void(0)" oncontextmenu="pupMenu(this)">右键出菜单1</a> &nbsp;&nbsp;&nbsp;
  		<a href="javascript:void(0)" oncontextmenu="pupMenu(this)">右键弹出菜单2</a>&nbsp;&nbsp;&nbsp;
  		<a href="javascript:void(0)" oncontextmenu="pupMenu(this)">右键弹出菜单3</a>	
  	</div>

 
</body>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 
  <script language="javascript">

  
	$(function() {
 
		//菜单上 添加单击事件 
		$('#custom_menu').find('a').bind('click',function(){
			delayTimeFun();
		});;
	});
	
    // 菜单上 点击事件 
    function delayTimeFun(){
        //执行父页面跳转 
        //window.parent.location="/test/test_delayAfterTime.action?"+$('#form').serialize();
        alert("点击了确定按钮");
        return false;
    }
 
 

	 //创建弹出菜单
	function  pupMenu( obj ){
	   $('#custom_menu').css({top:$.pageY,left:$.pageX});
	   $('#custom_menu').show();
	   //还原其他链接背景色
	    $("a").css("background-color","white");
	   //当前点击链接背景色为red
       $(obj).css("background-color","red");
 
	}

    //隐藏菜单
    function closeMenu( obj ){
    	$(obj).parent().hide();	
    	//还原 链接背景色
 	    $("a").css("background-color","white");
    }

    window.onload = function(){
    	preventEvent(  $('#useMenu').get(0) );
        var useMenu = $('#useMenu').find('a');
		for(var i=0;i<useMenu.length ;i++  ){
			addEvent( useMenu.get(i));
		}
		
		// 阻止 右键弹出 
		function preventEvent(htmlObj ){
	        if(htmlObj.addEventListener){
	        	htmlObj.addEventListener("contextmenu",function(event){
	                event.preventDefault();
	            },false);
	        }else{
		       
	        	htmlObj.attachEvent("oncontextmenu",function(event){
	               event = event || window.event;
	               event.returnValue = false;
	             }
	            );
	        }
		}

		//给对象添加事件 
		function addEvent(htmlObj ){
	        if(htmlObj.addEventListener){
	        	htmlObj.addEventListener("contextmenu",function(event){
	        		event.preventDefault();
	                menucon(event);
	            },false);
	        }else{
	        	htmlObj.attachEvent("oncontextmenu",function(event){
	               event = event || window.event;
	               event.returnValue = false;
	               menucon(event,htmlObj);
	             }
	            );
	        }
		}
        //菜单位置处理
        function menucon(event,htmlObj){
			
     	   $('#custom_menu').css({top:(event.clientY-10),left:event.clientX});
    	   $('#custom_menu').show();
    	   //还原其他链接背景色
    	    $("a").css("background-color","white");
    	   //当前点击链接背景色为red
           $(htmlObj).css("background-color","red");
           
        }
        
}




    
</script>

</html>

 

 

 

你可能感兴趣的:(JavaScript)