请大家看下如下的代码,我的希望是新增加的TD,点击的时候alert 显示的是1,2,3,但是目前代码alert出来都是3,应该怎么办呢?<html> 
<head> 
<title>Test</title> 
<script type="text/javascript"> 

function AddRow( table , rowHtml)
{
var TestTab=document.getElementById("TestTab");
var row = TestTab.insertRow(-1);
        row.insertCell(-1).innerHTML="123";
        row.insertCell(-1).innerHTML="456";
        row.insertCell(-1).innerHTML="759";
        for(var i=0; i<3; i++)
        {            
            row.cells[i].onclick = function(){
                 alert(i);
                 };            
        }

}
</script> 
</head> 
<body>    
<a href="javascript:void(0);" onclick="AddRow();" > 增加</a><table cellspacing="0" rules="all" bordercolor="#CCCCCC" border="1px" id="TestTab" 
style="border-color:#CCCCCC;border-width:1px;border-style:solid;width:98%;border-collapse:collapse;MARGIN: 5px 0px"> 
<tr> 
<td>A</td>
<td>B</td>
<td>C</td> 
</tr> 
</table> 
</body> 
</html> 

解决方案 »

  1.   


    <html> 
    <head> 
    <title>Test</title> 
    <script type="text/javascript">     
        
        function AddRow( table , rowHtml)
        {
            var TestTab=document.getElementById("TestTab");        
            var row = TestTab.insertRow(-1);
            row.insertCell(-1).innerHTML="123";
            row.insertCell(-1).innerHTML="456";
            row.insertCell(-1).innerHTML="759";
            for(var i=0; i<3; i++)
            {            
                row.cells[i].onclick = (function(i){
    return function(){
    alert(i+1);
    }                
                })(i);            
            }
            
        }
    </script> 
    </head> 
    <body>                                           
    <a href="javascript:void(0);" onclick="AddRow();" > 增加</a><table cellspacing="0" rules="all" bordercolor="#CCCCCC" border="1px" id="TestTab" 
    style="border-color:#CCCCCC;border-width:1px;border-style:solid;width:98%;border-collapse:collapse;MARGIN: 5px 0px"> 
    <tr> 
        <td>A</td>
        <td>B</td>
        <td>C</td> 
    </tr> 
    </table> 
    </body> 
    </html> 
      

  2.   


    // 或者
    <html> 
    <head> 
    <title>Test</title> 
    <script type="text/javascript">     
        
        function AddRow( table , rowHtml)
        {
            var TestTab=document.getElementById("TestTab");        
            var row = TestTab.insertRow(-1);
            row.insertCell(-1).innerHTML="123";
            row.insertCell(-1).innerHTML="456";
            row.insertCell(-1).innerHTML="759";
            for(var i=0; i<3; i++)
            {            
    row.cells[i]._index = i;
                row.cells[i].onclick = function(){
               alert(this._index + 1);
                };          
            }
            
        }
    </script> 
    </head> 
    <body>                                           
    <a href="javascript:void(0);" onclick="AddRow();" > 增加</a><table cellspacing="0" rules="all" bordercolor="#CCCCCC" border="1px" id="TestTab" 
    style="border-color:#CCCCCC;border-width:1px;border-style:solid;width:98%;border-collapse:collapse;MARGIN: 5px 0px"> 
    <tr> 
        <td>A</td>
        <td>B</td>
        <td>C</td> 
    </tr> 
    </table> 
    </body> 
    </html> 
      

  3.   

    你这个问题这有水平,ls高手已经给出了答案,原来-1代表:插入(行)单元格到 cells(rows) 集合内的最后一个。function AddRow( table , rowHtml)
        {
            var TestTab=document.getElementById("TestTab");        
            var row = TestTab.insertRow(-1);
            row.insertCell(-1).innerHTML="123";
            row.insertCell(-1).innerHTML="456";
            row.insertCell(-1).innerHTML="759";
               
                row.cells[0].onclick = function(){
                        alert(0);
                        };  
                row.cells[1].onclick = function(){
                        alert(1);
                        }; 
                row.cells[2].onclick = function(){
                        alert(2);
                        };                   
        }
    这样的话就会符合你的要求,回过头来看看,就会发现for(var i=0; i<3; i++)
            {            
                row.cells[i].onclick = function(){
                        alert(i);
                        };            
            }
    在这个循环里,alert(i)中的i由于上下文的原因,只能得到这个闭包的最后一个值,这就是问题的原因,解决方法按照
    s_liangchao1s 的解决就可以了