注册|登录

联系电话:024-31891684  13390130939
沈阳软件公司--沈阳软件定制

沈阳软件开发_沈阳软件公司_沈阳软件定制/软件/最新技术

Latest technology最新技术

利用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;
}

document.oncontextmenu = function (e)
{
_Hidden();
var _Obj = document.all ? event.srcElement : e.target;
if((_Type.indexOf(_Obj.type) == -1 || _Obj.type=="")&&_Obj.tagName!="TD") return;
_Amenu = _Obj.getAttribute('menu');
if(_Amenu == 'null'||_Amenu==null) return;
if(document.all) e = event;
_ShowMenu(_Amenu, e);
   permissionkey(); //判断权限
return false;
}
function permissionkey(){};
function _ShowMenu(Eid, event)
{
_Menu = document.getElementById(Eid);
var _Left = event.clientX + document.body.scrollLeft;
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;
}
function transfer(e){
e=e||window.event;e.cancelBubble=true;
}
 
function no_context_menu(e){
e=e||window.event;
e=e||window.event;e.cancelBubble=true;
return;
}
Css代码:
.DreamMenu {
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;
}
.DreamMenu ul {
margin: 1px;
border-bottom: buttonhighlight 1px solid;
border-top: buttonshadow 1px solid;
}
.DreamMenu a {
display: block;
width: 100%;
padding: 1px 2px 2px 20px;
cursor: default;
text-decoration: none;
color: #000000;
}
.DreamMenu a:hover {
background: highlight;
color: #ffffff;
}

沈阳团购网|营口网站制作|沈阳软件公司|软件定制|网站建设|加盟易势|提交问题