cnsd007 2009-06-08 14:24
浏览 317
已采纳

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]

  • 写回答

3条回答 默认 最新

  • cammy0301 2009-06-08 15:00
    关注

    [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]
    上面多了个加粗的代码,改了下!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R