Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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总是在总行中添加1_Javascript_Jquery - Fatal编程技术网

Javascript总是在总行中添加1

Javascript总是在总行中添加1,javascript,jquery,Javascript,Jquery,我对这个Javascript代码感到困惑,我用它来获取表中的总行数。它的输出总是超过1。示例:它将打印5而不是4 <script> (function() { var div = document.getElementById('divID11'); div.innerHTML = document.getElementById('tableId11').rows.length; })(); </script> <di

我对这个Javascript代码感到困惑,我用它来获取表中的总行数。它的输出总是超过1。示例:它将打印5而不是4

<script>
    (function() {
       var div = document.getElementById('divID11');
       div.innerHTML =  document.getElementById('tableId11').rows.length;
    })();
</script>

<div id =divID11></div>

(功能(){
var div=document.getElementById('divID11');
div.innerHTML=document.getElementById('tableId11').rows.length;
})();
表结构如下所示

<table id="tableId11>
 <thead>
  <tr>
   <th>
   </th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><?php echo $data ?></td>
  </tr>
 </tbody>

如果只想计算
TBODY
行,请使用以下JavaScript代码:

(function() {
    var div = document.getElementById('divID11');
    div.innerHTML =  document.getElementById('tableId11').getElementsByTagName("tbody")[0].rows.length;
})();
JavaScript代码计算表中的所有行(thead和tbody)。如果只想计算tbody行,则必须指定元素(因此必须修改代码以指定要使用表的哪个部分)

试试这个:

 var div = document.getElementById('divID11');
        div.innerHTML = document.getElementById('tableId1').getElementsByTagName('tbody')[0].rows.length;

你是在数THEAD中的行吗?你的代码对我来说很好@Thilo,我在计算Tbodys中的td,因为您在计算表中的所有行,您的代码运行良好。“我在计算tbody中的td”-至少在您发布的代码中没有。它正在计算所有行,包括thead中的行。你的建议对我很有用。现在我知道我的Javascript出了什么问题