我有一个表格是循环出来的、我想设计成当用户点击其中某一个tr时弹出一个层、这个层所显示在页面中的高度、要由tr的值决定、所以我需要获取每个循环出来tr的rowIndex 以方便计算div在屏幕上的位置、我根据网上的教程、现在获取到的rowIndex的值始终为0,请问对于while循环出来的tr难道每一行的rowIndex值都为0么?对于我这种需求还有其他处理方法么?
function popUserDetail(x,y){
var popUp = document.getElementById("popUserDetail");
    popUp.style.top="100px";
    popUp.style.left="150px";
    popUp.style.width="200px";
    popUp.style.height="100px";
    popUp.style.visibility="visible";
     alert(y.rowIndex);
     alert(x);
}
        while ($records = mysql_fetch_array($rows)) 
           {           
            $arr = ct($records['uniqueid']);
            $fileLastUploadTime  = $arr[1];
            print   '<table id="tb2">
                    <tr onclick="popUserDetail('.$records["uniqueid"].',this)" onmousemove="hidePopUserDetail()">
                   <td width="40px">'.st($records['lastT'],$fileLastUploadTime).'</td>
                    <td width="100px">'.$records['barName'].'</td>
                    <td width="160px">'.$records['lastLogin'].'</td>
                    <td width="160px">'.date("Y-m-d H:i:s",$arr[1]).'</td>
                    <td width="150px">'.$records['endTM'].'</td>
                    <td width="40px">'.$arr[0].'</td>
                    <td width="40px">'.$records['version'].'</td>
                    </tr>
                    </table>';
            } 

解决方案 »

  1.   

    不知道这样行不行 你用struts2标签还是jstl表达式啊???for each的时候用个计数器看得行不 或者自定义写个类
      

  2.   

    你的PHP代码每次循环都输出一个只有一行的表格,TableRow对象的rowIndex值自然都是0了。
      

  3.   


    不是说tr每次被创建都会生成一个rowIndex的嘛、我不是很理解、如果我要获取鼠标选择的当前行有其他方法可以处理么?
      

  4.   

    rowIndex 属性返回某一行在表格的行集合中的位置(row index)注意,这个索引号其实指的是该行在所属表格中的行索引,举个简单的例子就很清楚了:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head><body>
    <!--表1具有一行二列-->
    <table width="500" border="1">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table><!--表2具有二行二列-->
    <table width="500" border="1">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table><script type="text/javascript">
    //获取所有的tr节点,两个表格加起来一共有3行
    var obj = document.getElementsByTagName('tr');//遍历输出各行的rowIndex
    for (var i = 0; i < obj.length; i ++) alert(obj[i].rowIndex);
    /*运行结果:
    0 -- 第一个表格的第0行
    0 -- 第二个表格的第0行
    1 -- 第二个表格的第1行
    */
    </script>
    </body>
    </html>
      

  5.   

    非常感谢你细致的解释、一个人对着代码看一天也看不出来毛病、还是要有人点拨、我把<table>放到循环外解决了、感谢你的帮助、嘿嘿