在你的js中添加如下原形声明:
Function.prototype.bind   =   function()   {   
      var   __method   =   this;   
      var   arg   =   arguments;   
      return   function()   {   
          __method.apply(window,   arg);   
      }   
  }
然后,你可以这样来写你的代码
row.onclick = test.bind(i);

解决方案 »

  1.   

    function test(str) {
        alert(str);
    }var row = tbody.insertRow()
    row.onclick = function(){test(i);};
      

  2.   

    演示代码:a.html
    -------------------------------------
    <html>
    <script language=javascript>
    function test()
    {
    for(var i=0;i<10;i++)
    {
    var tr = document.all.tb.insertRow();
    var td = tr.insertCell();
    td.innerHTML = "这是第"+i+"行";
    tr.onclick = myfunc.bind(i,td);

    }
    }function myfunc(str,td)
    {
    alert(str+":"+td.innerHTML);
    }Function.prototype.bind   =   function()   {   
          var   __method   =   this;   
          var   arg   =   arguments;   
          return   function()   {   
              __method.apply(window,   arg);   
          }   
      }   
    </script>
    <body>
    <input type=button value=test onclick=test()><br>
    <table id=tb width=200 border=1>
    <tr><td bgcolor=yellow>这里是表头部分</td></tr>
    </table>
    </body>
    </html>
    -------------------------------------
      

  3.   

    To:saillovemeng
    function test(str) {
        alert(str);
    }var row = tbody.insertRow()
    row.onclick = function(){test(i);};
    ---------------------------------------------
    这个可以正确的给row的onclick绑定带参数的函数,但有个问题,如方便,请运行如下页面看效果,你可能会发现,绑定的参数都变成了最后一次绑定的内容,不晓得是为什么?
    -----------------------------------------------------------------------
    <html>
    <script language=javascript>
    function test()
    {
    for(var i=0;i<10;i++)
    {
    var tr = document.all.tb.insertRow();
    var td = tr.insertCell();
    td.innerHTML = "这是第"+i+"行";
    tr.onclick = function(){myfunc(i,td);};
    }
    }function myfunc(str,td)
    {
    alert(str+":"+td.innerHTML);
    }
      
    </script>
    <body>
    <input type=button value=test onclick=test()><br>
    <table id=tb width=200 border=1>
    <tr><td bgcolor=yellow>这里是表头部分</td></tr>
    </table>
    </body>
    </html>
    -----------------------------------------------------------------------
      

  4.   

    <html>
    <script language=javascript>
    function test()
    {
    alert('')
    for(var i=0;i<10;i++)
    {
    var tr = document.getElementById("tb").insertRow();
    var td = tr.insertCell();
    td.innerHTML = "aa"+i+"hang";
    tr.onclick = (function (i,td){return function(){myfunc(i,td);}})(i,td)
    }
    }function myfunc(str,td)
    {
    alert(str+":"+td.innerHTML);
    }</script>
    <body>
    <input type=button value=test onclick="test()"><br>
    <table id=tb width=200 border=1>
    <tr><td bgcolor=yellow>1111111111</td></tr>
    </table>
    </body>
    </html>查查闭包
      

  5.   

    我试了一下,确如FuWaer所说,使用塞尔的那个方法,会绑定最后一个值,FuWaer的那个方法是可以正确使用的。
    不过还是要感谢高手们的指点,谢了!
      

  6.   

    哦 这是什么原因呀
    我的网站 指点一下
    http://www.bework.com.cn
      

  7.   

    不好意思,我那样写确实会出现闭包现象
    请问mingxuan3000(铭轩)这种是什么意思,我还没用过这种写法。
    我觉得大概是函数一次性调用的方法,是不是?
    (function (i,td){return function(){myfunc(i,td);}})(i,td)