Javascript 如何为table.rows.length函数排除html表格中的标题行?
Java脚本中的table.rows.length返回表中的行数,包括Javascript 如何为table.rows.length函数排除html表格中的标题行?,javascript,html,Javascript,Html,Java脚本中的table.rows.length返回表中的行数,包括标记中的行数。我需要的是,如果有一个标题行(在中)只有元素,我想对该行进行负计数,我只需要其中包含数据的行的计数,而不需要标题元素 注意:我不能使用table.rows.length-1,因为我不能修改Javascript。我只能在HTML端修改。下面是代码示例片段。这里我需要算4而不是5 <html> <head> <meta http-equiv="Content-Type" content=
标记中的行数。我需要的是,如果有一个标题行(在
中)只有
元素,我想对该行进行负计数,我只需要其中包含数据的行的计数,而不需要标题元素
注意:我不能使用table.rows.length-1
,因为我不能修改Javascript。我只能在HTML端修改。下面是代码示例片段。这里我需要算4而不是5
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample</title>
<script type="text/javascript">
function call(){
alert(document.getElementById('tab').rows.length);
}
</script>
</head>
<body onload="call()">
<table id="tab" border="1">
<thead>
<th>
header 1
</th>
<th>
header 2
</th>
</thead>
<tbody>
<tr>
<td> data1</td>
<td> data1</td>
</tr>
<tr>
<td> data2</td>
<td> data2</td>
</tr>
<tr>
<td> data3</td>
<td> data3</td>
</tr>
<tr>
<td> data</td>
<td> data4</td>
</tr>
</tbody>
</table>
</body>
</html>
样品
函数调用(){
警报(document.getElementById('tab').rows.length);
}
标题1
标题2
数据1
数据1
数据2
数据2
数据3
数据3
数据
数据4
将id
重新分配给
元素:
<table border="1">
<tbody id="tab">
...
...
由于这个元素是
one,所以它也有行
(HTMLCollection)属性,所以您根本不需要在JS中进行任何更改