[原创]右键菜单 [好久没来了 随便灌一水]
程序代码:
/***************************************************/ /*******IE右键菜单 GreatBy Islet ************/ /********* 作用域 targetElement ************/ /********* 右键内容 Menus ************/ /********* 对象变量名 varMenu ************/ /***************************************************/ /* 定义右键菜单内容 二维数组 每个菜单的参数均为文本,依次为:[菜单显示文本,点击执行函数名及参数,不显示该菜单的校验(可写成函数,true为隐藏,false为显示,不隐藏直接给false即可)] 最后一个菜单对象设成检验函数,给出隐藏整个菜单的条件,false为显示,true为隐藏 menuArr = [['文本复制'],//复制菜单已经内置 安格式调用即可 ['用户信息查询' ,'OpenWin(10);' ,'"表箱总表|楼道灯".indexOf(datagrid.rows.item(datagrid.selectedIndex).data[5])>-1'], ['分隔符'], //分隔符已经内置 安格式调用即可 ['修改表箱信息' ,'OpenWin(1);' ,'"表箱总表|楼道灯".indexOf(datagrid.rows.item(datagrid.selectedIndex).data[5])<0'], ['换表查询' ,'OpenWin(11);' ,false], //始终显示该菜单 "DisplayMenu()" //验证是否隐藏菜单函数,true隐藏 false显示 ]; 调用方法 myrightdiv = new RightMenus(document.getElementById('sgcbDataGrid'),menuArr,"myrightdiv"); //不要加var */ /********开始***********************************************************/ //写入菜单样式 document.write('<style type="text/css"><!--\ .RightMenu { position:absolute; left:187px; top:48px;cursor:default;width:150; z-index:1000; border-top-width: 1px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #CCCCCC; border-right-color: #666666; border-bottom-color: #666666; border-left-color: #CCCCCC; padding:2;background-color: #FFFFFF;}\ .RightMenu button {font-size: 12px;line-height: 18px;color: #000000;background-color: #ffffff;border: none;width: 100%;text-align: left;padding-top: 0;padding-right: 7;padding-bottom: 0;padding-left: 7;margin-bottom: -2px;}\ --></style>') function RightMenus(targetElement,Menus,varMenu){ var Rinfo=''; if(typeof(Menus)=="object"){ for(var i=0;i<Menus.length-1;i++){ if(Menus[i]=="分隔符") Rinfo +='<hr id="rmenu'+i+'" size="2">'; else{ if(Menus[i]=="文本复制") Menus[i] = ["复制","textCopy()","getSelectedText()==''"]; Rinfo +='<button onClick="'+varMenu+'.RightMenuDisc();'+Menus[i][1]+';" \ onMouseOver="this.style.backgroundColor=\'#335EA8\'; this.style.color=\'#FFF\'" \ onMouseOut="this.style.backgroundColor=\'#FFF\'; this.style.color=\'#000\'" \ id="rmenu'+i+'">\ <font face="webdings">4</font> '+ Menus[i][0] +'\ </button>'; } } var createDiv = document.createElement("div"); document.body.insertBefore(createDiv,document.body.firstChild); createDiv.outerHTML='<div id="'+varMenu+'" class="RightMenu" style="display:none" onDblClick="'+varMenu+'.RightMenuDisc()">'+Rinfo+'</div>'; } this.RightMenuPlay = function(){//CheckFuncfion var o=document.getElementById(varMenu); o.style.display=""; bodyH = document.body.clientHeight; oH = o.clientHeight; bodyW = document.body.clientWidth; oW = o.clientWidth; y=event.clientY; x=event.clientX; if(bodyH-y-oH<0) y = y - oH + document.body.scrollTop; if(bodyW-x-oW<0) x = x - oW + document.body.scrollLeft; o.style.top = y; o.style.left = x; } this.RightMenuDisc = function(){ var o=document.getElementById(varMenu); o.style.display="none" } this.RightMenuShow = function(){ if(eval(Menus[Menus.length-1])) return; window.event.returnValue=false; var firstMenu=true; if(typeof(Menus)=="object"){ for(var i=0;i<Menus.length-1;i++) if(typeof(Menus[i])=="object" && Menus[i]!="分隔符") if(eval(Menus[i][2])) document.getElementById('rmenu'+i).style.display="none"; else{ firstMenu=false; document.getElementById('rmenu'+i).style.display=""; } else if(Menus[i]=="分隔符" && (i==Menus.length-2 || firstMenu)) document.getElementById('rmenu'+i).style.display="none"; else document.getElementById('rmenu'+i).style.display=""; } eval(varMenu + ".RightMenuPlay()"); } targetElement.oncontextmenu = this.RightMenuShow; if(document.onclick==null) document.onclick=this.RightMenuDisc; if(targetElement.onclick==null) targetElement.onclick=this.RightMenuDisc; } //实现文本复制 function textCopy(){ document.execCommand("Copy"); //执行浏览器复制命令 } function getSelectedText() { //获取被选择文字 var selected = ''; var sel = document.selection; var rng = sel.createRange(); rng.colapse; if((sel.type == "Text" || sel.type == "None") && rng != null){ if(rng.text.length > 0) selected = rng.text; } return selected; } /********结束***********************************************************/