日期:2014-05-16  浏览次数:20335 次

JS操作table问题
当我删除 TableID.deleteRow(rowIndex); 删除行的之后。
该如何更新其他行的rowIndex号?
JScript code

//初始化行,设置序列号;   
function initRows(tab) {
    for (var i = 0; i < tab.rows.length; i++) {
        tab.rows[i].rowIndex = i; //这里报错了。 提示 对象不支持此操作
    }
}

请问该如何重新初始化所有的行号?



------解决方案--------------------
rowIndex是只读属性,删除行以后不需要重新“初始化”,浏览器会自动重新计算各TableRow对象的rowIndex值。
HTML code
<!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>
<script type="text/javascript">
window.onload = function() {
    var t = document.getElementsByTagName('table')[0];
    t.deleteRow(1);
    alert(t.rows[1].cells[0].innerHTML);
}
</script>
</head>

<body>
<table width="500" border="1">
  <tr>
    <td>R1</td>
  </tr>
  <tr>
    <td>R2</td>
  </tr>
  <tr>
    <td>R3</td>
  </tr>
</table>
</body>
</html>