to wuxinlangman(无心之尘 8.14号开始学习js技术不过关不要见怪) : 这是我全部的代码,我把服务器部分都去掉了,可直接粘贴保存,你看在你那里是否能正常运行,请帮忙再指点一下.谢谢.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>TestMenu</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script language=javascript>
function TestMe() { alert("hi"); }
function showMenu(i) { var oPopup=window.createPopup(); var myDiv=document.getElementById("menuItem"+i); oPopup.document.body.innerHTML = myDiv.innerHTML; oPopup.show(-5, 17, 108, 110,document.getElementById("menu"+i)); // alert(i); }
function LoadMenu() { myStr="站点信息/基本信息^SiteInfo.aspx|参数列表^SitePara.aspx|预警方案^SiteAlarmProgram.aspx|工艺图片^SiteCraftImg.aspx|站点图片^SiteImg.aspx@"; myStr+="实时数据^RTData.aspx@"; myStr+="数据查询/查询表单^SearchData.aspx|查询图表^SearchData_Chart.aspx@"; myStr+="数据报表/日报表^Report_Air.aspx|周报表^Report_Air.aspx|月报表^Report_Air.aspx|年报表^Report_Air.aspx@"; myStr+="数据比较^SiteDataCmp.aspx";
if (myStr!=null) { var MenuAList=new Array(); MenuAList=myStr.split("@"); for(var i=1;i<=MenuAList.length;i++) { var MenuHeader=MenuAList[i-1].split("/"); var menuName="menu"+i; var menuText=MenuHeader[0].split("^"); if (menuText.length>1) //没能子项 { document.getElementById(menuName).innerHTML="<a href='"+menuText[1]+"'>"+menuText[0]+"</a>"; } else //有子项 { document.getElementById(menuName).innerHTML=menuText[0]; //菜单根 var showDiv=document.getElementById("menuItem"+i);
var menuItem=MenuHeader[1].split("|"); var menuDiv=document.createElement("div"); menuDiv.style.background="#c7e5ff"; menuDiv.style.width="110px"; menuDiv.style.height="120px"; for (var j=0;j<menuItem.length;j++) { var menuItemText=new Array(); menuItemText=menuItem[j].split("^"); var menuItemDiv=document.createElement("span"); menuItemDiv.innerHTML=" "+menuItemText[0]; // menuItemDiv.innerHTML=" <a href='aa.aspx'>"+menuItemText[0]+"</a>"; // menuItemDiv.onclick=TestMe(); menuItemDiv.style.font="9pt 宋体"; menuItemDiv.style.cursor="hand"; menuItemDiv.style.height="20px"; menuItemDiv.style.width="105px";
function showMenu(i) { var oPopup=window.createPopup(); var myDiv=document.getElementById("menuItem"+i); oPopup.document.body.innerHTML = myDiv.innerHTML; oPopup.show(-5, 17, 108, 110,document.getElementById("menu"+i)); // alert(i); }
function LoadMenu() { myStr="站点信息/基本信息^SiteInfo.aspx|参数列表^SitePara.aspx|预警方案^SiteAlarmProgram.aspx|工艺图片^SiteCraftImg.aspx|站点图片^SiteImg.aspx@"; myStr+="实时数据^RTData.aspx@"; myStr+="数据查询/查询表单^SearchData.aspx|查询图表^SearchData_Chart.aspx@"; myStr+="数据报表/日报表^Report_Air.aspx|周报表^Report_Air.aspx|月报表^Report_Air.aspx|年报表^Report_Air.aspx@"; myStr+="数据比较^SiteDataCmp.aspx";
if (myStr!=null) { var MenuAList=new Array(); MenuAList=myStr.split("@"); for(var i=1;i<=MenuAList.length;i++) { var MenuHeader=MenuAList[i-1].split("/"); var menuName="menu"+i; var menuText=MenuHeader[0].split("^"); if (menuText.length>1) //没能子项 { document.getElementById(menuName).innerHTML="<a href='"+menuText[1]+"'>"+menuText[0]+"</a>"; } else //有子项 { document.getElementById(menuName).innerHTML=menuText[0]; //菜单根 var showDiv=document.getElementById("menuItem"+i);
var menuItem=MenuHeader[1].split("|"); var menuDiv=document.createElement("div"); menuDiv.style.background="#c7e5ff"; menuDiv.style.width="110px"; menuDiv.style.height="120px"; for (var j=0;j<menuItem.length;j++) { var menuItemText=new Array(); menuItemText=menuItem[j].split("^"); var menuItemDiv=document.createElement("div"); menuItemDiv.innerHTML=" "+menuItemText[0]; // menuItemDiv.innerHTML=" <a href='aa.aspx'>"+menuItemText[0]+"</a>"; // menuItemDiv.onclick=TestMe(); menuItemDiv.style.font="9pt 宋体"; menuItemDiv.style.cursor="hand"; menuItemDiv.style.height="20px"; menuItemDiv.style.width="105px";
你说的是什么意思啊.这不跟我写的一样嘛.
menuDiv.appendChild(menuItemDiv);
换下位置看看
---------------------------
menuItemDiv.onclick=test;
{
alert("hi");
}
去掉test之后的()。原因:带括号的话,test()会在attachEvent之前执行,然后test()的返回值会作为onclick的处理函数。所以出现,你所说的在关联的时间就触发了事件,在点击时却没反应。请注意:test和test()的区别。test表示代表一个函数或变量(没有区别在js里);test()则调用test函数。
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function aa(){
var menuDiv=document.createElement("div");
menuDiv.style.background="#c7e5ff";
menuDiv.style.width="110px";
menuDiv.style.height="120px";
var menuItem="1,2,3,4";
for (var j=0;j<3;j++)
{
var menuItemText=new Array();
menuItemText=menuItem.split(",");
var menuItemDiv=document.createElement("span");
menuItemDiv.innerHTML=" "+menuItemText[0];
menuItemDiv.style.font="9pt 宋体";
menuItemDiv.style.cursor="hand";
menuItemDiv.style.height="20px";
menuItemDiv.style.width="110px";
menuItemDiv.attachEvent("onclick",function(){alert("111");});
menuDiv.appendChild(menuItemDiv);
}
document.getElementById("div1").appendChild(menuDiv);
}//问题就在这里.
// menuItemDiv.onClick=function(){test()}; //这样写也不行.
// menuItemDiv.onClick=test(); //这样写在加载的时直接触发事件.
//-->
</SCRIPT>
</HEAD><BODY onload="aa()">
<div id="div1"></div>
</BODY>
</HTML>
谢谢你的帮助,我把你的例子都试了,可是还是没有触发事件啊.呜呜.
这是我全部的代码,我把服务器部分都去掉了,可直接粘贴保存,你看在你那里是否能正常运行,请帮忙再指点一下.谢谢.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>TestMenu</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function TestMe()
{
alert("hi");
}
function showMenu(i)
{
var oPopup=window.createPopup();
var myDiv=document.getElementById("menuItem"+i);
oPopup.document.body.innerHTML = myDiv.innerHTML;
oPopup.show(-5, 17, 108, 110,document.getElementById("menu"+i));
// alert(i);
}
function LoadMenu()
{
myStr="站点信息/基本信息^SiteInfo.aspx|参数列表^SitePara.aspx|预警方案^SiteAlarmProgram.aspx|工艺图片^SiteCraftImg.aspx|站点图片^SiteImg.aspx@";
myStr+="实时数据^RTData.aspx@";
myStr+="数据查询/查询表单^SearchData.aspx|查询图表^SearchData_Chart.aspx@";
myStr+="数据报表/日报表^Report_Air.aspx|周报表^Report_Air.aspx|月报表^Report_Air.aspx|年报表^Report_Air.aspx@";
myStr+="数据比较^SiteDataCmp.aspx";
if (myStr!=null)
{
var MenuAList=new Array();
MenuAList=myStr.split("@");
for(var i=1;i<=MenuAList.length;i++)
{
var MenuHeader=MenuAList[i-1].split("/");
var menuName="menu"+i;
var menuText=MenuHeader[0].split("^");
if (menuText.length>1) //没能子项
{
document.getElementById(menuName).innerHTML="<a href='"+menuText[1]+"'>"+menuText[0]+"</a>";
}
else //有子项
{
document.getElementById(menuName).innerHTML=menuText[0]; //菜单根
var showDiv=document.getElementById("menuItem"+i);
var menuItem=MenuHeader[1].split("|");
var menuDiv=document.createElement("div");
menuDiv.style.background="#c7e5ff";
menuDiv.style.width="110px";
menuDiv.style.height="120px";
for (var j=0;j<menuItem.length;j++)
{
var menuItemText=new Array();
menuItemText=menuItem[j].split("^");
var menuItemDiv=document.createElement("span");
menuItemDiv.innerHTML=" "+menuItemText[0];
// menuItemDiv.innerHTML=" <a href='aa.aspx'>"+menuItemText[0]+"</a>";
// menuItemDiv.onclick=TestMe();
menuItemDiv.style.font="9pt 宋体";
menuItemDiv.style.cursor="hand";
menuItemDiv.style.height="20px";
menuItemDiv.style.width="105px";
menuItemDiv.onclick=function(){window.alert("aa")};
menuItemDiv.attachEvent("onClick",TestMe);
menuItemDiv.onclick=TestMe;
menuItemDiv.onClick=function(){window.alert("aa")};
menuItemDiv.attachEvent("onclick",function(){alert("111");});
menuDiv.appendChild(menuItemDiv);
}
/* var menuStr;
menuStr="<DIV style='BACKGROUND: #c7e5ff;border-color:#000000;WIDTH: 110px;HEIGHT: 120px'>"
for (var j=0;j<menuItem.length;j++)
{
var menuItemText=new Array();
menuItemText=menuItem[j].split("^");
menuStr+="<DIV id='divItem' style='FONT: 9pt 宋体; CURSOR: hand;HEIGHT: 20px' onclick=window.location('aa.aspx')> "+menuItemText[0]+"</DIV>"
}
menuStr+="<div>"
*//* menuStr="<table width='122' border='0' cellpadding='0' cellspacing='0' class='borderTop borderLeft borderRight borderBottom'>"
for (var j=0;j<menuItem.length;j++)
{
var menuItemText=new Array();
menuItemText=menuItem[j].split("^");
menuStr+="<tr><td width='17' height='20'></td><td width='105'><a href='"+menuItemText[1]+"'>"+menuItemText[0]+"</a></td></tr>";
}
var sender=document.getElementById(menuName); */ document.getElementById(menuName).onclick=function(){showMenu(i)};
switch (i)
{
case 1:
document.getElementById(menuName).onclick=function(){showMenu(1)};
break;
case 2:
document.getElementById(menuName).onclick=function(){showMenu(2)};
break;
case 3:
document.getElementById(menuName).onclick=function(){showMenu(3)};
break
case 4:
document.getElementById(menuName).onclick=function(){showMenu(4)};
break;
case 5:
document.getElementById(menuName).onclick=function(){showMenu(5)};
break;
} showDiv.appendChild(menuDiv);
}
}
}
}
</script>
</HEAD>
<body>
<table cellSpacing="0" cellPadding="0" width="697" border="0" ID="Table1">
<tr>
<td align="right" width="17"><IMG height="20" src="../Images_Blue/menuline_start.jpg" width="6"></td>
<td width="669" background="../Images_Blue/menuline_back.jpg">
<table cellSpacing="0" cellPadding="0" width="650" border="0" ID="Table2">
<tr>
<td width="60">
<div class="mouseHand" id="menu1"></div>
</td>
<td width="2"></td>
<td width="60">
<div class="mouseHand" id="menu2"></div>
</td>
<td width="2"></td>
<td width="60">
<div class="mouseHand" id="menu3"></div>
</td>
<td width="2"></td>
<td width="60">
<div class="mouseHand" id="menu4"></div>
</td>
<td width="2"></td>
<td width="60">
<div class="mouseHand" id="menu5"></div>
</td>
</tr>
</table>
</td>
<td width="11"><IMG height="20" src="../Images_Blue/menuline_end.jpg" width="6"></td>
</tr>
</table>
<P> </P>
<P> </P>
<P><br>
<INPUT id="hidden1" type=hidden NAME="hidden1"><INPUT id="hidden2" type=hidden NAME="hidden2"><INPUT id="hidden3" type=hidden NAME="hidden3"><INPUT id="hidden4" type=hidden NAME="hidden4"><INPUT id="hidden5" type=hidden NAME="hidden5"></P> <div id="menuItem1" style="DISPLAY: none; FONT-SIZE: 9pt; BORDER-LEFT-COLOR: #1186db; BORDER-BOTTOM-COLOR: #1186db; WIDTH: 130px; COLOR: #000000; BORDER-TOP-COLOR: #1186db; FONT-FAMILY: @宋体; LETTER-SPACING: 1px; BACKGROUND-COLOR: #c7e5ff; BORDER-RIGHT-COLOR: #1186db"></div>
<div class="menuStyle" id="menuItem2" style="DISPLAY: none"></div>
<div class="menuStyle" id="menuItem3" style="DISPLAY: none"></div>
<div class="menuStyle" id="menuItem4" style="DISPLAY: none"></div>
<div class="menuStyle" id="menuItem5" style="DISPLAY: none"></div>
<script language="javascript">LoadMenu()</script>
</body>
</HTML>
___________
我怎么用这句出不来啊!!!
你是怎么出来的啊!!!
<HEAD>
<title>TestMenu</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function TestMe()
{
alert("hi");
}
function showMenu(i)
{
var oPopup=window.createPopup();
var myDiv=document.getElementById("menuItem"+i);
oPopup.document.body.innerHTML = myDiv.innerHTML;
oPopup.show(-5, 17, 108, 110,document.getElementById("menu"+i));
// alert(i);
}
function LoadMenu()
{
myStr="站点信息/基本信息^SiteInfo.aspx|参数列表^SitePara.aspx|预警方案^SiteAlarmProgram.aspx|工艺图片^SiteCraftImg.aspx|站点图片^SiteImg.aspx@";
myStr+="实时数据^RTData.aspx@";
myStr+="数据查询/查询表单^SearchData.aspx|查询图表^SearchData_Chart.aspx@";
myStr+="数据报表/日报表^Report_Air.aspx|周报表^Report_Air.aspx|月报表^Report_Air.aspx|年报表^Report_Air.aspx@";
myStr+="数据比较^SiteDataCmp.aspx";
if (myStr!=null)
{
var MenuAList=new Array();
MenuAList=myStr.split("@");
for(var i=1;i<=MenuAList.length;i++)
{
var MenuHeader=MenuAList[i-1].split("/");
var menuName="menu"+i;
var menuText=MenuHeader[0].split("^");
if (menuText.length>1) //没能子项
{
document.getElementById(menuName).innerHTML="<a href='"+menuText[1]+"'>"+menuText[0]+"</a>";
}
else //有子项
{
document.getElementById(menuName).innerHTML=menuText[0]; //菜单根
var showDiv=document.getElementById("menuItem"+i);
var menuItem=MenuHeader[1].split("|");
var menuDiv=document.createElement("div");
menuDiv.style.background="#c7e5ff";
menuDiv.style.width="110px";
menuDiv.style.height="120px";
for (var j=0;j<menuItem.length;j++)
{
var menuItemText=new Array();
menuItemText=menuItem[j].split("^");
var menuItemDiv=document.createElement("div");
menuItemDiv.innerHTML=" "+menuItemText[0];
// menuItemDiv.innerHTML=" <a href='aa.aspx'>"+menuItemText[0]+"</a>";
// menuItemDiv.onclick=TestMe();
menuItemDiv.style.font="9pt 宋体";
menuItemDiv.style.cursor="hand";
menuItemDiv.style.height="20px";
menuItemDiv.style.width="105px";
//menuItemDiv.onclick=function(){window.alert("aa")};
//menuItemDiv.attachEvent("onClick",TestMe);
//menuItemDiv.onclick=TestMe;
menuItemDiv.onClick="parent.TestMe()";
//menuItemDiv.attachEvent('onclick',TestMe);
menuDiv.appendChild(menuItemDiv);
}
/* var menuStr;
menuStr="<DIV style='BACKGROUND: #c7e5ff;border-color:#000000;WIDTH: 110px;HEIGHT: 120px'>"
for (var j=0;j<menuItem.length;j++)
{
var menuItemText=new Array();
menuItemText=menuItem[j].split("^");
menuStr+="<DIV id='divItem' style='FONT: 9pt 宋体; CURSOR: hand;HEIGHT: 20px' onclick=window.location('aa.aspx')> "+menuItemText[0]+"</DIV>"
}
menuStr+="<div>"
*//* menuStr="<table width='122' border='0' cellpadding='0' cellspacing='0' class='borderTop borderLeft borderRight borderBottom'>"
for (var j=0;j<menuItem.length;j++)
{
var menuItemText=new Array();
menuItemText=menuItem[j].split("^");
menuStr+="<tr><td width='17' height='20'></td><td width='105'><a href='"+menuItemText[1]+"'>"+menuItemText[0]+"</a></td></tr>";
}
var sender=document.getElementById(menuName); */
switch (i)
{
case 1:
document.getElementById(menuName).onclick=function(){showMenu(1)};
break;
case 2:
document.getElementById(menuName).onclick=function(){showMenu(2)};
break;
case 3:
document.getElementById(menuName).onclick=function(){showMenu(3)};
break
case 4:
document.getElementById(menuName).onclick=function(){showMenu(4)};
break;
case 5:
document.getElementById(menuName).onclick=function(){showMenu(5)};
break;
} showDiv.appendChild(menuDiv);
}
}
}
}
</script>
</HEAD>
<body>
<table cellSpacing="0" cellPadding="0" width="697" border="0" ID="Table1">
<tr>
<td align="right" width="17"><IMG height="20" src="../Images_Blue/menuline_start.jpg" width="6"></td>
<td width="669" background="../Images_Blue/menuline_back.jpg">
<table cellSpacing="0" cellPadding="0" width="650" border="0" ID="Table2">
<tr>
<td width="60">
<div class="mouseHand" id="menu1"></div>
</td>
<td width="2"></td>
<td width="60">
<div class="mouseHand" id="menu2"></div>
</td>
<td width="2"></td>
<td width="60">
<div class="mouseHand" id="menu3"></div>
</td>
<td width="2"></td>
<td width="60">
<div class="mouseHand" id="menu4"></div>
</td>
<td width="2"></td>
<td width="60">
<div class="mouseHand" id="menu5"></div>
</td>
</tr>
</table>
</td>
<td width="11"><IMG height="20" src="../Images_Blue/menuline_end.jpg" width="6"></td>
</tr>
</table>
<P> </P>
<P> </P>
<P><br>
<INPUT id="hidden1" type=hidden NAME="hidden1"><INPUT id="hidden2" type=hidden NAME="hidden2"><INPUT id="hidden3" type=hidden NAME="hidden3"><INPUT id="hidden4" type=hidden NAME="hidden4"><INPUT id="hidden5" type=hidden NAME="hidden5"></P> <div id="menuItem1" style="DISPLAY: none; FONT-SIZE: 9pt; BORDER-LEFT-COLOR: #1186db; BORDER-BOTTOM-COLOR: #1186db; WIDTH: 130px; COLOR: #000000; BORDER-TOP-COLOR: #1186db; FONT-FAMILY: @宋体; LETTER-SPACING: 1px; BACKGROUND-COLOR: #c7e5ff; BORDER-RIGHT-COLOR: #1186db"></div>
<div class="menuStyle" id="menuItem2" style="DISPLAY: none"></div>
<div class="menuStyle" id="menuItem3" style="DISPLAY: none"></div>
<div class="menuStyle" id="menuItem4" style="DISPLAY: none"></div>
<div class="menuStyle" id="menuItem5" style="DISPLAY: none"></div>
<script language="javascript">LoadMenu()</script>
</body>
</HTML>
to:wuxinlangman(无心之尘 8.14号开始学习js技术不过关不要见怪)
太感激了,这位兄台,一直为我的这个问题忙了这么长时间,这就给分,一百分全是你的!