我用下面的代码给我的web页面添加了一个右键菜单,(页面加载时调用了showMenu()函数,oncontextmenu = showMenu(''))但这个菜单无论在页面的什么地方点击都会出现,我只想在用右键点击dtree的节点的时候才让菜单出现,而且点击的节点不同,出现的菜单也不一样,谁能告诉我该怎么做呀?<script language="javascript">
//页面加载时调用此函数
function showMenu(id) {
if(id == "") { //默认点击页面空白处时的右键菜单
popMenu(itemMenu,110,"100"); //默认只显示“新增”菜单项
//menuForm.addNode.value = "/ipmanage/addNode.jsp"; //注意ipmanage目录下需要已经创建了一个addNode.jsp页面
//menuForm.delNode.value = "";
//menuForm.updateNode.value = "";
}else{ //根据特定的字符串解析
var temparr = new Array();
//temparr = temp.split(",");
// menuForm.addNode.value = temparr[0];
//menuForm.delNode.value = temparr[1];
//menuForm.updateNode.value = temparr[2];
popMenu(itemMenu,110,id);
}
event.returnValue=false;
event.cancelBubble=true;
return false;
}
//弹出右键菜单
function popMenu(menuDiv,width,rowControlString)
{
//创建弹出菜单
var pop=window.createPopup();
//设置弹出菜单的内容
pop.document.body.innerHTML=menuDiv.innerHTML; //这句话控制了弹出菜单的html代码为定义右键菜单的div的html代码,即弹出时显示div中的表格
var rowObjs=pop.document.body.all[0].rows; //获得表格中的行对象
//获得弹出菜单的行数
var rowCount=rowObjs.length;
//循环设置每行的属性
for(var i=0;i<rowObjs.length;i++)
{
//如果设置该行不显示,则行数减一
var hide=rowControlString.charAt(i)!='1';
if(hide){
rowCount--;
}
//设置是否显示该行
rowObjs[i].style.display=(hide)?"none":"";
//设置鼠标滑入该行时的效果
rowObjs[i].cells[0].onmouseover=function()
{
this.style.background="#E8E8E8";
this.style.color="blue";
}
//设置鼠标滑出该行时的效果
rowObjs[i].cells[0].onmouseout=function(){
this.style.background="#D0D0D0";
this.style.color="black";
}
}
//屏蔽系统的Web页面本身的右键菜单
pop.document.oncontextmenu=function() {
return false;
}
//选择右键菜单的一项后,菜单隐藏
pop.document.onclick=function() {
pop.hide();
}
//显示右键菜单
pop.show(event.clientX-1,event.clientY,width,rowCount*20,document.body);
return true;
}
</script><div id="itemMenu" style="display:none">
<table border="1" width="100%" bgcolor="#D0D0D0" style="border:thin" cellspacing="0">
<tr>
<td style="cursor:hand;border:outset 1;font-size:12px" align="left" onclick="parent.addNode()">新增</td>
</tr>
<tr>
<td style="cursor:hand;border:outset 1;font-size:12px" align="left" onclick="parent.delNode()">删除</td>
</tr> <tr>
<td style="cursor:hand;border:outset 1;font-size:12px" align="left" onclick="parent.updateNode()">更新</td>
</tr>
</table>
</div>
//页面加载时调用此函数
function showMenu(id) {
if(id == "") { //默认点击页面空白处时的右键菜单
popMenu(itemMenu,110,"100"); //默认只显示“新增”菜单项
//menuForm.addNode.value = "/ipmanage/addNode.jsp"; //注意ipmanage目录下需要已经创建了一个addNode.jsp页面
//menuForm.delNode.value = "";
//menuForm.updateNode.value = "";
}else{ //根据特定的字符串解析
var temparr = new Array();
//temparr = temp.split(",");
// menuForm.addNode.value = temparr[0];
//menuForm.delNode.value = temparr[1];
//menuForm.updateNode.value = temparr[2];
popMenu(itemMenu,110,id);
}
event.returnValue=false;
event.cancelBubble=true;
return false;
}
//弹出右键菜单
function popMenu(menuDiv,width,rowControlString)
{
//创建弹出菜单
var pop=window.createPopup();
//设置弹出菜单的内容
pop.document.body.innerHTML=menuDiv.innerHTML; //这句话控制了弹出菜单的html代码为定义右键菜单的div的html代码,即弹出时显示div中的表格
var rowObjs=pop.document.body.all[0].rows; //获得表格中的行对象
//获得弹出菜单的行数
var rowCount=rowObjs.length;
//循环设置每行的属性
for(var i=0;i<rowObjs.length;i++)
{
//如果设置该行不显示,则行数减一
var hide=rowControlString.charAt(i)!='1';
if(hide){
rowCount--;
}
//设置是否显示该行
rowObjs[i].style.display=(hide)?"none":"";
//设置鼠标滑入该行时的效果
rowObjs[i].cells[0].onmouseover=function()
{
this.style.background="#E8E8E8";
this.style.color="blue";
}
//设置鼠标滑出该行时的效果
rowObjs[i].cells[0].onmouseout=function(){
this.style.background="#D0D0D0";
this.style.color="black";
}
}
//屏蔽系统的Web页面本身的右键菜单
pop.document.oncontextmenu=function() {
return false;
}
//选择右键菜单的一项后,菜单隐藏
pop.document.onclick=function() {
pop.hide();
}
//显示右键菜单
pop.show(event.clientX-1,event.clientY,width,rowCount*20,document.body);
return true;
}
</script><div id="itemMenu" style="display:none">
<table border="1" width="100%" bgcolor="#D0D0D0" style="border:thin" cellspacing="0">
<tr>
<td style="cursor:hand;border:outset 1;font-size:12px" align="left" onclick="parent.addNode()">新增</td>
</tr>
<tr>
<td style="cursor:hand;border:outset 1;font-size:12px" align="left" onclick="parent.delNode()">删除</td>
</tr> <tr>
<td style="cursor:hand;border:outset 1;font-size:12px" align="left" onclick="parent.updateNode()">更新</td>
</tr>
</table>
</div>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货