menuItemDiv.attachEvent("onclick",test);  //试试问题

解决方案 »

  1. to :kabakaba(echo "kabakaba";)
    你说的是什么意思啊.这不跟我写的一样嘛.
      

  2. menuItemDiv.attachEvent("onclick",test());  //问题就在这里
    menuDiv.appendChild(menuItemDiv);
    换下位置看看
      

  3. try
    ---------------------------
    menuItemDiv.onclick=test;
      

  4. 我的test里很简单,只是想先试一下能否关联上.    function test()
        {
          alert("hi");
        }
      

  5. 应该错在这里:menuItemDiv.attachEvent("onclick",test()),
    去掉test之后的()。原因:带括号的话,test()会在attachEvent之前执行,然后test()的返回值会作为onclick的处理函数。所以出现,你所说的在关联的时间就触发了事件,在点击时却没反应。请注意:test和test()的区别。test表示代表一个函数或变量(没有区别在js里);test()则调用test函数。
      

  6. <HTML>
    <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="&nbsp;&nbsp;"+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>
      

  7. 楼上的说的是要么你把方法写进取要么就写menuItemDiv.attachEvent("onclick",test)
      

  8. 谢谢WolfChaseLion(龙),你让我明白了一些东东,可是我去掉括号后还是无法触发test事件.
      

  9. menuItemDiv.attachEvent("onclick",function(){alert("111");});这样尝试一下
      

  10. 谢谢wuxinlangman(无心之尘 8.14号开始学习js技术不过关不要见怪) :
    谢谢你的帮助,我把你的例子都试了,可是还是没有触发事件啊.呜呜.
      

  11. 我用的是IE 6.0.3790.1830 ,我用换了同事xp的机子访问我电脑上的网页,结果还是结果一样,狂晕.我用的是win2003.
      

  12. 我把Ajax的东东去掉再试下看看.
      

  13. 如果是的话建议你再读取的时候先把返回的值中的js代码eval一下
      

  14. 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="&nbsp;&nbsp;"+menuItemText[0];               
                //   menuItemDiv.innerHTML="&nbsp;&nbsp;<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')>&nbsp;&nbsp;"+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>&nbsp;</P>
    <P>&nbsp;</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>
      

  15. 能否把我的代码直接粘贴到本地,保存成Html文件,然后运行看下,能否正常,不行的话,按你的意思做些个改动,看是否实现,谢了.
      

  16. menuItemDiv.onClick="parent.TestMe()";
      

  17. menuItemDiv.onClick="parent.TestMe()";
    ___________
    我怎么用这句出不来啊!!!
    你是怎么出来的啊!!!
      

  18. 就再你的onclick那里添加上menuItemDiv.onClick="parent.TestMe()";
      

  19. <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="&nbsp;&nbsp;"+menuItemText[0];               
                //   menuItemDiv.innerHTML="&nbsp;&nbsp;<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')>&nbsp;&nbsp;"+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>&nbsp;</P>
    <P>&nbsp;</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>
      

  20. 哈!解决啦!!谢谢!!
    to:wuxinlangman(无心之尘 8.14号开始学习js技术不过关不要见怪)
    太感激了,这位兄台,一直为我的这个问题忙了这么长时间,这就给分,一百分全是你的!
      

类似问题 »