eval(arrayB[t]);改为eval(eval("aryCode["+i+"]"))

解决方案 »

  1.   

    1,2楼都不对,我再重贴一遍代码,红色是需要修改部分,请大家发帖的时候麻烦测试下<script> 
    var aryName=["从小到大排序","从大到小排序","取消"]; 
    var aryCode=["alert('1')","alert('2')","alert('3')"]; 
    function DrawDiv(arrayA,arrayB,obj){ 
          var aryElement = []; 
      var br = document.createElement("br"); 
          var thediv = document.createElement(" <div name='customerDiv'>"); 
      thediv.style.width = 120; 
      thediv.style.height = 100; 
                              thediv.style.left = 500; 
      thediv.style.top = 600; 
                              thediv.style.zIndex = 50; 
      thediv.style.position = "absolute"; 
                              thediv.style.border = 1; 
      thediv.style.borderStyle = "solid"; 
      thediv.style.borderColor ="#DDD" 
      for (i=0;i <arrayA.length;i++){ 
          aryElement[i] = document.createElement(" <a name='customerDivA'>"); 
      aryElement[i].style.display="block" 
                                  aryElement[i].innerHTML = arrayA[i]; 
      aryElement[i].onclick = function(){ 
          eval(arrayB[i]); 
      } 
                                  thediv.appendChild(aryElement[i]); 
      }   document.body.appendChild(thediv); 
      } 
    </script> <input type="button" value="按钮" onclick="DrawDiv(aryName,aryCode,this)"> 
      

  2.   

    <script> 
    var aryName=["从小到大排序","从大到小排序","取消"]; 
    var aryCode=["alert('1')","alert('2')","alert('3')"]; 
    function DrawDiv(arrayA,arrayB,obj){ 
    var aryElement = []; 
    var br = document.createElement("br"); 
    var thediv = document.createElement(" <div name='customerDiv'>"); 
    thediv.style.width = 120; 
    thediv.style.height = 100; 
    thediv.style.left = 500; 
    thediv.style.top = 600; 
    thediv.style.zIndex = 50; 
    thediv.style.position = "absolute"; 
    thediv.style.border = 1; 
    thediv.style.borderStyle = "solid"; 
    thediv.style.borderColor ="#DDD" 
    for (i=0;i <arrayA.length;i++){ 
    aryElement[i] = document.createElement(" <a name='customerDivA'>"); 
    aryElement[i].style.display="block" 
    aryElement[i].innerHTML = arrayA[i]; 
    (function(i){
    aryElement[i].onclick = function(){ 
    eval(arrayB[i]); 
    }
    })(i)
    thediv.appendChild(aryElement[i]); 


    document.body.appendChild(thediv); 

    </script> <input type="button" value="按钮" onclick="DrawDiv(aryName,aryCode,this)"> 
      

  3.   


        var aryName=["从小到大排序","从大到小排序","取消"];
        var aryCode=["alert('1')","alert('2')","alert('3')"];
        function DrawDiv(arrayA,arrayB,obj)
        {
            var aryElement = [];
            var br = document.createElement("br");
            var thediv = document.createElement("div");
            thediv.setAttribute("name","customerDiv");
            thediv.style.width = 120;
            thediv.style.height = 100;
            thediv.style.left = 500;
            thediv.style.top = 600;
            thediv.style.zIndex = 50;
            thediv.style.position = "absolute";
            thediv.style.border = 1;
            thediv.style.borderStyle = "solid";
            thediv.style.borderColor ="#DDD"
            for (i=0;i <arrayA.length;i++)
            {
                var t = i;
                aryElement[i] = document.createElement("a");
                aryElement[i].setAttribute("name","customerDivA");
                aryElement[i].style.display="block";
                aryElement[i].innerHTML = arrayA[i];
                aryElement[i].onclick = function()
                {
                    eval(arrayB[t]);
                }
                thediv.appendChild(aryElement[i]);
            }        document.body.appendChild(thediv);
        } 
      

  4.   

    4楼好强,不过没看懂你写的,4楼修改代码如下:aryElement[i].onclick = function(){ 
          eval(arrayB[i]); 
      } 
     改为:
    (function(i){
            aryElement[i].onclick = function(){ 
                eval(arrayB[i]); 
            }
            })(i)哪位高人能解释下4楼的代码吗?不胜感激另外,5楼的帖不知所谓,name属性是不可被setAttribute的,这是常识。
      

  5.   

    没人解释下?aryElement[i].onclick = function(){ 
          eval(arrayB[i]); 
      } 
    改为: 
    (function(i){ 
            aryElement[i].onclick = function(){ 
                eval(arrayB[i]); 
            } 
            })(i) 
      

  6.   

    aryElement[i].onclick = function(){ 
          eval(arrayB[i]); 
      } 
    改为: 
    (function(i){ 
            aryElement[i].onclick = function(){ 
                eval(arrayB[i]); 
            } 
            })(i) 比如第一个a,在为它创建onclick的时候i的值是0;但是当运行onclick的时候i的值已经改变(for中的i++),用(function(i){ })(i) 可以说是保持现场吧