论坛首页 Web前端技术论坛

右键菜单的实现

浏览 5119 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-10  

下面是简单的写了下。复杂的话请联系我.282215036

 

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
 <title>右键菜单功能</title>
<style>
.menu {
z-index:990;
border-right: black 2px outset;
border-top: black 1px outset;
visibility: hidden;
background:#EEE;
border-left: black 1px solid;
width: 170px;
cursor: hand;
line-height: 20px;
padding-top: 2px;
border-bottom: black 2px solid;
position: absolute;

text-align: left;
font-size: 12px;
margin: 10px 0px 0px;
font-family: "宋体";
}

.menuitems {
padding-right: 1px;
padding-left: 10px;
padding-bottom: 2px;
padding-top: 2px;
}
</style>
<script>
function highlightie(evt, classItems) {
   var event = evt || window.event;
   var element = event.srcElement || event.target;
   if (element.className == classItems) {
     element.style.backgroundColor = "#336699";
     element.style.outlineColor = "red";
     element.style.color = "white";
   }
}

function lowlightie(evt, classItems) {
   var event = evt || window.event;
   var element = event.srcElement || event.target;
   if (element.className == classItems) {
      element.style.backgroundColor = "";
      element.style.color = "black";
   }
}
 
var menuskin = "menu";
function showMenu() {
  var dbcw = document.body.clientWidth, dbch = document.body.clientHeight;
  var dbsl = document.body.scrollLeft, dbst = document.body.scrollTop;
  var event=getEvent();
  var rightedge = dbcw-event.clientX;
  var bottomedge = dbch-event.clientY;
  main_menu.style.display = ""; 
  if (rightedge < main_menu.offsetWidth)  {
     main_menu.style.left = (dbcw >= main_menu.offsetWidth) ? (dbcw - main_menu.offsetWidth) : 0;
  } else {
     main_menu.style.left = dbsl + event.clientX;
  }
  if (bottomedge < main_menu.offsetHeight) {
     main_menu.style.top = (dbch >= main_menu.offsetHeight) ? (dbch - main_menu.offsetHeight) : 0;
  } else {
     main_menu.style.top = dbst + event.clientY;
  }

  main_menu.style.visibility = "visible";
  event.returnValue = false;
  return false;
}   

function getEvent() {  
   if (document.all) {
  return window.event;
   }
   func=getEvent.caller;           
   while(func != null) {   
       var arg0=func.arguments[0];
       if(arg0) {
          if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
             || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)) {   
              return arg0;
          }
       }
       func=func.caller;
   }
   return null;
}

function hideAllMenu() {
  main_menu.style.display = "none";
}

function $(id){
 return document.getElementById(id);
}

function add() {
alert("小样,成功了!");
}

</script>
</head>
<body>
<div class="menu" id="main_menu" onmouseover="highlightie(event, 'menuitems');" onmouseout="lowlightie(event, 'menuitems');">
<div class="menuitems" onclick="add()">&nbsp;添加(点一下)</div>
<div class="menuitems">&nbsp;删除</div>
<hr />
<div class="menuitems">&nbsp;上移</div>
<div class="menuitems">&nbsp;下移</div>
</div>
<table width="2200" oncontextmenu="showMenu()";>
 <tr>
  <td width="180px"><input type="button" value="my name is xi pei" style="width:180px;"></td>
 </tr>
</table>
</body>
<script>
var main_menu = $("main_menu");
main_menu.className = menuskin;
document.body.onclick = hideAllMenu;
</script>
</html>

  • xp.rar (5.4 KB)
  • 下载次数: 306
   发表时间:2009-08-20  
ie8不可以啊
0 请登录后投票
   发表时间:2009-08-20  
不好意思啊,在IE8上面我还没有测试过!
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics