JS高手看看吧,右键菜单中传递对象名的问题? function document.onmouseover(){ menutitle = window.event.toElement.innerText; //得到鼠标当前所对应的文字,以便判断是否弹出菜单} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的代码有三处错误:1.ie5menu_style应该写成:ie5menu.style2.两个层的名称重了,而且和一个连接的名称相同,所以在你的函数中是用group得到的是一个数组ie5menu.style是空值。3.document.oncontextmenu=showmenuie5(menuName)document.oncontextmenu=""这样写是错误的,改:document.oncontextmenu=function(){showmenuie5(menuName);}document.oncontextmenu=null; 其他还有一些小问题,我帮你该了,这是该后的程序:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>右键菜单</title><SCRIPT LANGUAGE="JavaScript"><!-- // RightClickMenudocument.oncontextmenu=function(){return false;};function showmenuie5(menuName){ var ie5menu=eval(menuName) var rightedge=document.body.clientWidth-event.clientX var bottomedge=document.body.clientHeight-event.clientY if (rightedge<ie5menu.offsetWidth) ie5menu.style.left=document.body.scrollLeft+event.clientX-ie5menu.offsetWidth else ie5menu.style.left=document.body.scrollLeft+event.clientX if (bottomedge<ie5menu.offsetHeight) ie5menu.style.top=document.body.scrollTop+event.clientY-ie5menu.offsetHeight else ie5menu.style.top=document.body.scrollTop+event.clientY ie5menu.style.visibility="visible" return false}function hidemenuie5(menuName){ var ie5menu=eval(menuName) ie5menu.style.visibility="hidden"}function highlightie5(itemName){ if (event.srcElement.className==itemName){ event.srcElement.style.backgroundColor="highlight" event.srcElement.style.color="white" }}function lowlightie5(itemName){ if (event.srcElement.className==itemName){ event.srcElement.style.backgroundColor="" event.srcElement.style.color="black" }} function jumptoie5(itemName){ if (event.srcElement.className==itemName){ if (event.srcElement.url != ''){ if (event.srcElement.getAttribute("target")!=null) window.open(event.srcElement.url,event.srcElement.getAttribute("target")) else window.location=event.srcElement.url } }}// --></SCRIPT><STYLE TYPE='text/css'> .cMenu{ position: absolute; visibility:hidden; color:#000000; width:200px; border:2px solid #000000; background-color:#999999; font-size:14px; font-weight:normal; line-height:20px; cursor:default;}.menuitems{ padding-left:15px; padding-right:10px;}</STYLE></head><body><p> <a href="javascript:void();" name="user" id="user" onMouseOver="rightmenu('group1')" onMouseOut="rightmenu_clear();">用户</a></p><a href="javascript:void();" name="group" id="group" onMouseOver="rightmenu('group2');" onMouseOut="rightmenu_clear();">组</a><p></p><RightClick> <!--[if IE]><div id="group1" name="group1" class="cMenu" onMouseover="highlightie5('usermenuitems')" onMouseout="lowlightie5('usermenuitems')" onClick="jumptoie5('usermenuitems')"><div class="usermenuitems" url="javascript:alert ('usercreate')">创建用户</div><div class="usermenuitems" url="javascript:alert ('usermodify')">修改用户</div><div class="usermenuitems" url="javascript:alert ('userdel')">删除用户</div><![endif]--><!--[if IE]><div id="group2" name="group2" class="cMenu" onMouseover="highlightie5('groupmenuitems')" onMouseout="lowlightie5('groupmenuitems')" onClick="jumptoie5('groupmenuitems')"><div class="groupmenuitems" url="javascript:alert ('usercreate')">创建组</div><div class="groupmenuitems" url="javascript:alert ('usermodify')">修改组</div><div class="groupmenuitems" url="javascript:alert ('userdel')">删除组</div><![endif]--><script language="JavaScript"><!--function rightmenu(menuName){ if (document.all&&window.print){ ie5menu=eval(menuName) //alert (ie5menu) ie5menu.className="cMenu" document.oncontextmenu=function(){showmenuie5(menuName);return false;} document.body.onclick=function(){hidemenuie5(menuName);} }}function rightmenu_clear(){ document.oncontextmenu=function(){return false;}; document.onclick=null;}//--></script></RightClick> </body></html> <!-- 时间较紧,改得仓促! --><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style>td {font-size: 13px}.menu1 {cursor: default}</style><script language=Javascript>var menuclass,menutitle;function document.onmouseover(){ menuclass = window.event.toElement.className; menutitle = window.event.toElement.innerText; //得到鼠标当前所对应的文字,以便判断是否弹出菜单 }function document.onmousedown(){ var x = window.event.clientX; var y = window.event.clientY; var x0=document.body.clientWidth; var y0=document.body.clientHeight; if(event.button==2 && (menutitle=="连接一" || menutitle=="连接二")){ ie5menu.style.display=""; var x1=document.all.ie5menu.clientWidth; var y1=document.all.ie5menu.clientHeight; ie5menu.style.left = (x0-x-4<x1) ? x-x1-4 : x+2; ie5menu.style.top = (y0-y<y1) ? y-y1-4 : y; //这种位置设置法的好处在于碰到不够宽的时候菜单会真实window菜单一样移位 } if(event.button==1 && menuclass != "menu1") ie5menu.style.display = "none";}function jumptoie5(){ if (event.srcElement.className=="menu1"){ if (event.srcElement.getAttribute("target")!=null) window.open(event.srcElement.url,event.srcElement.getAttribute("target")) else window.location=event.srcElement.url } ie5menu.style.display = "none"; }function mo(tt,st){ //当鼠标经过时会变到的底色与文字颜色 if (tt=="over"){st.background='highlight';st.color='#FFFFFF';} else {st.background='menu'; st.color='#000000';} }</SCRIPT></head><body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 oncontextmenu="window.event.returnValue=false;//取消鼠标右键菜单" onkeydown="if(event.keyCode==27)document.all.ie5menu.style.display='none';//ESC键隐藏菜单"><br><br><br><br> <a href="http://www.sina.com.cn">连接一</a><br><br> <a href="http://www.163.com">连接二</a><DIV id=ie5menu style="BACKGROUND: menu; border: 2 outset #FFFFFF;z-index: 12; position: absolute; display: none" onclick="jumptoie5()"><table border=0 cellspacing=2 cellpadding=1><tr><td noWrap class=menu1 onmouseover=mo("over",this.style) onmouseout=mo("out",this.style) url="addGroup.htm" target="main"> 添加子组 </td></tr><tr><td noWrap class=menu1 onmouseover=mo("over",this.style) onmouseout=mo("out",this.style) url="delGroup.jsp" target="main"> 修改该组</td></tr><tr><td noWrap class=menu1 onmouseover=mo("over",this.style) onmouseout=mo("out",this.style) url="#"> 删除该组</td></tr></table></div></body></html> 多谢各位的热心!再问一个问题:如果我要动态生成右键菜单怎么办?如-------------用户 用户1 用户2组 组1 组2-------------树图是从数据库中动态生成的,现在要求,在“用户1”中点右键,需传用户ID到URL中。如url="modify.asp?id=1"或者 "modify.asp?id=2",组也一样。应该是用innerHTML方法吧,不过我不太熟悉JS和DHTML:( 不太明白你的意思。“传用户ID到URL中”:这样页面就刷新了,干吗还要点右健呢?做个连接不就行了。 to: fokker(独孤龙) SORRY,意思没有表达清楚,需求是这样的:如-------------用户 用户1 用户2组 组1 组2-------------树图是从数据库中动态生成的,在任一用户上点击右键,出现“创建用户,修改用户,删除用户”菜单不同用户的“修改用户”和“删除用户”菜单的URL是不同的,如modify.asp?id=1 or id=2 or id=3,ID号是从数据库是取的用户ID号。现在的关键是下面这段,我想动态生成,就是指向不同链接时才产生DIV,这样就可将ID值传过去了<!--[if IE]><div id="group" name="group" class="cMenu" onMouseover="highlightie5('usermenuitems')" onMouseout="lowlightie5('usermenuitems')" onClick="jumptoie5('usermenuitems')"><div class="usermenuitems" url="create.asp">创建用户</div><div class="usermenuitems" url="modify.asp?id="+id>修改用户</div><div class="usermenuitems" url="del.asp?id="+id>删除用户</div><![endif]--> 如果是asp可以这样做:<div class="usermenuitems" url="modify.asp?id=<%=id%>>修改用户</div>其中<%=id%>是asp的变量 如果有100个用户,岂不是要在页面中写100个<DIV> 多谢大家的帮助,问题我已经解决了,直接用innerHTMl重写DIV即可。 请问JS或者JQ如何控制动态产生的元素的CSS属性? 简单的ajax安全问题,进来指导下 请教一个jquery切换背景的问题。 请问个事件处理程序括号的问题 getJSON为什么不能赋值出来? 关于JS输入限制问题 js解密高手帮忙给解密一下这段代码 向父窗口传递值 多选框的值 求一正则表达式,急,请各位师 请问谁知道怎么控制web页面的背景音乐? 当我点击一个下拉列表内容,另一个下拉列表的内容会相应改变,应该怎样做? 如何用javascript读取源文件!在线等!!!!!!!急
1.
ie5menu_style应该写成:ie5menu.style
2.
两个层的名称重了,而且和一个连接的名称相同,所以在你的函数中是用group得到的是一个数组ie5menu.style是空值。
3.
document.oncontextmenu=showmenuie5(menuName)
document.oncontextmenu=""
这样写是错误的,改:
document.oncontextmenu=function(){showmenuie5(menuName);}
document.oncontextmenu=null;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>右键菜单</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- // RightClickMenu
document.oncontextmenu=function(){return false;};function showmenuie5(menuName){
var ie5menu=eval(menuName)
var rightedge=document.body.clientWidth-event.clientX
var bottomedge=document.body.clientHeight-event.clientY
if (rightedge<ie5menu.offsetWidth)
ie5menu.style.left=document.body.scrollLeft+event.clientX-ie5menu.offsetWidth
else
ie5menu.style.left=document.body.scrollLeft+event.clientX
if (bottomedge<ie5menu.offsetHeight)
ie5menu.style.top=document.body.scrollTop+event.clientY-ie5menu.offsetHeight
else
ie5menu.style.top=document.body.scrollTop+event.clientY
ie5menu.style.visibility="visible"
return false
}
function hidemenuie5(menuName){
var ie5menu=eval(menuName)
ie5menu.style.visibility="hidden"
}
function highlightie5(itemName){
if (event.srcElement.className==itemName){
event.srcElement.style.backgroundColor="highlight"
event.srcElement.style.color="white"
}
}
function lowlightie5(itemName){
if (event.srcElement.className==itemName){
event.srcElement.style.backgroundColor=""
event.srcElement.style.color="black"
}
}
function jumptoie5(itemName){
if (event.srcElement.className==itemName){
if (event.srcElement.url != ''){
if (event.srcElement.getAttribute("target")!=null)
window.open(event.srcElement.url,event.srcElement.getAttribute("target"))
else
window.location=event.srcElement.url
}
}
}
// -->
</SCRIPT>
<STYLE TYPE='text/css'>
.cMenu{
position: absolute;
visibility:hidden;
color:#000000;
width:200px;
border:2px solid #000000;
background-color:#999999;
font-size:14px;
font-weight:normal;
line-height:20px;
cursor:default;
}.menuitems{
padding-left:15px;
padding-right:10px;
}
</STYLE>
</head><body>
<p> <a href="javascript:void();" name="user" id="user" onMouseOver="rightmenu('group1')" onMouseOut="rightmenu_clear();">用户</a></p>
<a href="javascript:void();" name="group" id="group" onMouseOver="rightmenu('group2');" onMouseOut="rightmenu_clear();">组</a>
<p></p>
<RightClick>
<!--[if IE]>
<div id="group1" name="group1" class="cMenu" onMouseover="highlightie5('usermenuitems')" onMouseout="lowlightie5('usermenuitems')" onClick="jumptoie5('usermenuitems')">
<div class="usermenuitems" url="javascript:alert ('usercreate')">创建用户</div>
<div class="usermenuitems" url="javascript:alert ('usermodify')">修改用户</div>
<div class="usermenuitems" url="javascript:alert ('userdel')">删除用户</div>
<![endif]-->
<!--[if IE]>
<div id="group2" name="group2" class="cMenu" onMouseover="highlightie5('groupmenuitems')" onMouseout="lowlightie5('groupmenuitems')" onClick="jumptoie5('groupmenuitems')">
<div class="groupmenuitems" url="javascript:alert ('usercreate')">创建组</div>
<div class="groupmenuitems" url="javascript:alert ('usermodify')">修改组</div>
<div class="groupmenuitems" url="javascript:alert ('userdel')">删除组</div>
<![endif]-->
<script language="JavaScript">
<!--
function rightmenu(menuName){
if (document.all&&window.print){
ie5menu=eval(menuName)
//alert (ie5menu)
ie5menu.className="cMenu"
document.oncontextmenu=function(){showmenuie5(menuName);return false;}
document.body.onclick=function(){hidemenuie5(menuName);}
}
}
function rightmenu_clear(){
document.oncontextmenu=function(){return false;};
document.onclick=null;
}
//-->
</script>
</RightClick>
</body>
</html>
<html><head><title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
td {font-size: 13px}
.menu1 {cursor: default}
</style>
<script language=Javascript>
var menuclass,menutitle;
function document.onmouseover(){
menuclass = window.event.toElement.className;
menutitle = window.event.toElement.innerText; //得到鼠标当前所对应的文字,以便判断是否弹出菜单
}
function document.onmousedown(){
var x = window.event.clientX;
var y = window.event.clientY;
var x0=document.body.clientWidth;
var y0=document.body.clientHeight; if(event.button==2 && (menutitle=="连接一" || menutitle=="连接二")){
ie5menu.style.display="";
var x1=document.all.ie5menu.clientWidth;
var y1=document.all.ie5menu.clientHeight;
ie5menu.style.left = (x0-x-4<x1) ? x-x1-4 : x+2;
ie5menu.style.top = (y0-y<y1) ? y-y1-4 : y;
//这种位置设置法的好处在于碰到不够宽的时候菜单会真实window菜单一样移位
}
if(event.button==1 && menuclass != "menu1") ie5menu.style.display = "none";
}function jumptoie5(){
if (event.srcElement.className=="menu1"){
if (event.srcElement.getAttribute("target")!=null)
window.open(event.srcElement.url,event.srcElement.getAttribute("target"))
else
window.location=event.srcElement.url
}
ie5menu.style.display = "none";
}function mo(tt,st){ //当鼠标经过时会变到的底色与文字颜色
if (tt=="over"){st.background='highlight';st.color='#FFFFFF';}
else {st.background='menu'; st.color='#000000';}
}
</SCRIPT>
</head>
<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 oncontextmenu="window.event.returnValue=false;//取消鼠标右键菜单"
onkeydown="if(event.keyCode==27)document.all.ie5menu.style.display='none';//ESC键隐藏菜单">
<br><br><br><br>
<a href="http://www.sina.com.cn">连接一</a><br><br>
<a href="http://www.163.com">连接二</a><DIV id=ie5menu style="BACKGROUND: menu; border: 2 outset #FFFFFF;z-index: 12; position: absolute; display: none" onclick="jumptoie5()">
<table border=0 cellspacing=2 cellpadding=1>
<tr><td noWrap class=menu1 onmouseover=mo("over",this.style) onmouseout=mo("out",this.style) url="addGroup.htm" target="main"> 添加子组 </td></tr>
<tr><td noWrap class=menu1 onmouseover=mo("over",this.style) onmouseout=mo("out",this.style) url="delGroup.jsp" target="main"> 修改该组</td></tr>
<tr><td noWrap class=menu1 onmouseover=mo("over",this.style) onmouseout=mo("out",this.style) url="#"> 删除该组</td></tr>
</table></div></body>
</html>
再问一个问题:
如果我要动态生成右键菜单怎么办?
如
-------------
用户
用户1
用户2
组
组1
组2
-------------
树图是从数据库中动态生成的,
现在要求,在“用户1”中点右键,需传用户ID到URL中。如url="modify.asp?id=1"或者 "modify.asp?id=2",组也一样。
应该是用innerHTML方法吧,不过我不太熟悉JS和DHTML:(
“传用户ID到URL中”:这样页面就刷新了,干吗还要点右健呢?做个连接不就行了。
如
-------------
用户
用户1
用户2
组
组1
组2
-------------
树图是从数据库中动态生成的,
在任一用户上点击右键,出现“创建用户,修改用户,删除用户”菜单
不同用户的“修改用户”和“删除用户”菜单的URL是不同的,如modify.asp?id=1 or id=2 or id=3,ID号是从数据库是取的用户ID号。
现在的关键是下面这段,我想动态生成,就是指向不同链接时才产生DIV,这样就可将ID值传过去了<!--[if IE]>
<div id="group" name="group" class="cMenu" onMouseover="highlightie5('usermenuitems')" onMouseout="lowlightie5('usermenuitems')" onClick="jumptoie5('usermenuitems')">
<div class="usermenuitems" url="create.asp">创建用户</div>
<div class="usermenuitems" url="modify.asp?id="+id>修改用户</div>
<div class="usermenuitems" url="del.asp?id="+id>删除用户</div>
<![endif]-->
<div class="usermenuitems" url="modify.asp?id=<%=id%>>修改用户</div>其中<%=id%>是asp的变量