function Table_KK(tableId)
        {
            
            this.ID=tableId;
            this.TableRows=document.getElementById(tableId).rows;
            this.SelectedRowIndex=-1;            for(var i=0; i < this.TableRows.length ; i++)
            {
                document.getElementById(tableId).rows[i].onclick=function(){this.SelectedRowIndex=i;
                    alert(tableObj.SelectedRowIndex);
                };
            }
        }  我想要实现 在点tr的时候 把tr的rowIndex的值 赋给SelectedRowIndex

解决方案 »

  1.   

    if(document.addEventListener){//FF
    object.addEventListener('click', function(){fun(2)}, false);
    }else if(attachEvent){//IE
    object.attachEvent('onclick', new Function("fun("+2+")"));
    }
    object是document.getElementById("id")获取的对象
      

  2.   

    典型的闭包问题
    function bind(i)
    {
     return function(){alert(i);}
    }function Table_KK(tableId)
      {
        
      this.ID=tableId;
      this.TableRows=document.getElementById(tableId).rows;
      this.SelectedRowIndex=-1;  for(var i=0; i < this.TableRows.length ; i++)
      {
      document.getElementById(tableId).rows[i].onclick=bind(i);
      };
      }
      }   
      

  3.   

    谢谢上面
    问题是 怎么给 this.SelectedRowIndex 赋值
    加事件能加上
      

  4.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">function aaa(tid){
      
      var rows = document.getElementById(tid).rows;
      var SelectedRowIndex = -1;
      
      for (i = 0; i < rows.length; i++) { 
    rows[i].onclick = function() { 
    SelectedRowIndex = eval(this.rowIndex + 1);
    alert(SelectedRowIndex); 

      }  
    }   
    </script>
     
    <body  onload="aaa('t1')">
    <table id="t1">
    <tr ><td> 1</td></tr>
    <tr ><td> 2</td></tr>
    <tr ><td> 3</td></tr>
    <tr ><td> 4</td></tr>
    <tr ><td> 5</td></tr> 
    </table>
    </body>
    </html>
      

  5.   

    var tableObj = document.getElementById(tableId);
    function Table_KK(tableId)
      {
       
      this.ID=tableId;
      this.TableRows=document.getElementById(tableId).rows;
      this.SelectedRowIndex=-1;  for(var i=0; i < this.TableRows.length ; i++)
      {
      document.getElementById(tableId).rows[i].onclick=function(){
    tableObj.SelectedRowIndex=i;
      alert(tableObj.SelectedRowIndex);
      };
      }
      }
      

  6.   

    楼主你可以这样:
    for(var i = 0; i < this.TableRows.length; i ++){
    document.getElementById("hidden").value = i;
    document.getElementById(tableId).rows[i].onclick = function(){
    this.SelectedRowIndex = document.getElementById("hidden").value;
    };
    }也就是将值赋给一个隐藏域,然后再函数里面再取出来
      

  7.   

    把SelectedRowIndex 在函数外面 用 var定义可以实现功能的
    变通用其他方法能实现我从网上看说 JavaScript 可以实现类似 面向对象的
    我想着把 SelectedRowIndex 当成它的一个属性 然后再里面用
    这样看着比较舒服一点
      

  8.   

    如此小的功能还面向对象?那不是自找麻烦么,再说了,那样的话,楼主的直接onclick赋值函数是不行的,按2楼的做,将参数传进去