利用JS实现WEB程序中的上下文菜单
浏览量:4114
上下文菜单在CS结构中经常出现,现因项目需求,需要在BS结构中实现上下文菜单,在网上查找案例,再经过自己改进,终于达到项目要求,现贴出代码
html页面加入代码:
<div id="menu" class='DreamMenu' style="width:100px;">
<a href='javascript:void(0);' onclick='edit();' id="aedit">
编辑</a>
<ul>
</ul>
<a href='javascript:void(0);' onclick='delall();' id="adel">
删除</a>
<ul>
</ul>
<a href='javascript:void(0);' onclick='show();' id="adetail">
详情</a>
</div>
js页代码:
if(!document.all) document.captureEvents(Event.MOUSEDOWN);
var _Tmenu = 0;
var _Amenu = 0;
var _Type = 'checkbox';
var _Menu = "null";
document.onclick = _Hidden;
function _Hidden()
{
if(_Tmenu==0) return;
document.getElementById(_Tmenu).style.visibility='hidden';
_Tmenu=0;
}
{
_Hidden();
var _Obj = document.all ? event.srcElement : e.target;
_Amenu = _Obj.getAttribute('menu');
if(document.all) e = event;
_ShowMenu(_Amenu, e);
permissionkey(); //判断权限
}
function permissionkey(){};
function _ShowMenu(Eid, event)
{
_Menu = document.getElementById(Eid);
var _Top = event.clientY + document.body.scrollTop;
_Menu.style.left = _Left.toString() + 'px';
_Menu.style.top = _Top.toString() + 'px';
_Menu.style.visibility = 'visible';
_Tmenu = Eid;
_Menu.onclick = transfer;
_Menu.oncontextmenu = no_context_menu;
}
e=e||window.event;e.cancelBubble=true;
}
function no_context_menu(e){
e=e||window.event;
e=e||window.event;e.cancelBubble=true;
return;
}
position: absolute;
visibility: hidden;
z-index: 100;
overflow: hidden;
width: 70px;
background-color: buttonface;
border: dimgray 1px solid !important;
border: buttonhighlight menu menu buttonhighlight 2px outset;
padding: 1px !important;
padding: 1px 1px 1px 0px;
font-size: 12px;
}
margin: 1px;
border-bottom: buttonhighlight 1px solid;
border-top: buttonshadow 1px solid;
}
display: block;
width: 100%;
padding: 1px 2px 2px 20px;
cursor: default;
text-decoration: none;
color: #000000;
}
background: highlight;
color: #ffffff;
}