javascript关于闭包的操作

[code="js"]<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



Insert title here
<p>function test(){</p> <p>var btBody = document.getElementById(&#39;test44&#39;).childNodes[1];<br> var ids=[1,2,3,6,9,20,21,34,45,67,68];<br> for(var i = 0 ; i&lt; ids.length ; i++){<br> var tr = document.createElement(&#39;tr&#39;);<br> var td = document.createElement(&#39;td&#39;);<br> var td1 = td.cloneNode(false);<br> td1.appendChild(document.createTextNode(ids[i]));<br> var td2 = td.cloneNode(false);<br> td2.appendChild(document.createTextNode(ids[i]+&quot;ssss&quot;));<br> var td3 = td.cloneNode(false);<br> td3.appendChild(document.createTextNode(ids[i]+&quot;aaaaa&quot;));<br> tr.appendChild(td1);<br> tr.appendChild(td2);<br> tr.appendChild(td3);<br> //tr.attachEvent(&quot;onclick&quot;,function(){<br> // return function (i){alert(i);}<br> // })(i); <br> tr.attachEvent(&quot;onclick&quot;,function(){<br> alert(i);<br> });<br><br> btBody.appendChild(tr);<br> }<br> }</p> <p>


    <table id="test44" cellspacing="1" border="1" class="tablesorter">
        <thead>
            <tr>
                <th>
                    First Name
                </th>
                <th>
                    Second Name
                </th>
                <th>
                    Age
                </th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
</body>

[/code]

这是testPage 我想给table的每一个行加上点击事件,点击一行,就要弹出这行是第几行,但是一直是得到的最后一行数据,要用闭包来保存数据,大虾们,怎么写恩
[b]问题补充:[/b]
谢谢大虾们。问题解决了恩

[code="js"]tr.attachEvent("onclick",function(i){ return function(){alert(i)}}(i));
// tr.onclick=function(i){return function(){alert(i)}}(i)[/code]

[code="java"]<%@ page language="java" contentType="text/html; charset=gbk"

pageEncoding="gbk"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">









Insert title here

<br><br> $(document).ready(function(){<br><br> $(&quot;th&quot;).click(function(){<br><br> alert($(&quot;th&quot;).index(this)); });<br><br> }); </p> <p>



    <table id="test44" cellspacing="1" border="1" class="tablesorter">      
        <thead>    
            <tr id="test">      
                <th>      
                    First Name      
                </th>      
                <th>      
                    Second Name      
                </th>      
                <th>      
                    Age      
                </th>      
            </tr>      
        </thead>      
        <tbody>      

        </tbody>      
    </table>      
</body>      

[/code]
上面多了个加粗的代码,改了下!

[code="java"]
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



Insert title here
<p>function test(){</p> <p>var btBody = document.getElementById(&#39;test44&#39;).childNodes[1];<br> var ids=[1,2,3,6,9,20,21,34,45,67,68];<br> for(var i = 0 ; i&lt; ids.length ; i++){<br> (function(i){<br> var tr = document.createElement(&#39;tr&#39;);<br> var td = document.createElement(&#39;td&#39;);<br> var td1 = td.cloneNode(false);<br> td1.appendChild(document.createTextNode(ids[i]));<br> var td2 = td.cloneNode(false);<br> td2.appendChild(document.createTextNode(ids[i]+&quot;ssss&quot;));<br> var td3 = td.cloneNode(false);<br> td3.appendChild(document.createTextNode(ids[i]+&quot;aaaaa&quot;));<br> tr.appendChild(td1);<br> tr.appendChild(td2);<br> tr.appendChild(td3);<br> tr.attachEvent(&quot;onclick&quot;,function(){<br> alert(i);<br> });<br><br> btBody.appendChild(tr);<br> })(i);<br> }<br> }</p> <p>


    <table id="test44" cellspacing="1" border="1" class="tablesorter">
        <thead>
            <tr>
                <th>
                    First Name
                </th>
                <th>
                    Second Name
                </th>
                <th>
                    Age
                </th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
</body>

[/code]

你现在还在用原始js写哦,代码好多哦,用jquery吧,很简单的!如果用jquery的话用下面的方法就可以了!
[code="java"]
<%@ page language="java" contentType="text/html; charset=gbk"

pageEncoding="gbk"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">








Insert title here

<br><br> $(document).ready(function(){<br> [b]$(&quot;th&quot;).click(function(){<br> alert($(&quot;th&quot;).index(this));[/b] });<br> });</p> <p>



    <table id="test44" cellspacing="1" border="1" class="tablesorter">   
        <thead> 
            <tr id="test">   
                <th>   
                    First Name   
                </th>   
                <th>   
                    Second Name   
                </th>   
                <th>   
                    Age   
                </th>   
            </tr>   
        </thead>   
        <tbody>   

        </tbody>   
    </table>   
</body>   

[/code]

代码就2行