Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为table.rows.length函数排除html表格中的标题行?_Javascript_Html - Fatal编程技术网

Javascript 如何为table.rows.length函数排除html表格中的标题行?

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=

Java脚本中的table.rows.length返回表中的行数,包括
标记中的行数。我需要的是,如果有一个标题行(在
中)只有
元素,我想对该行进行负计数,我只需要其中包含数据的行的计数,而不需要标题元素

注意:我不能使用
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中进行任何更改