如何从表中获取除标题行之外的所有行?

时间:2014-11-11 09:07:08

标签: javascript html

我在JavaScript中动态创建了表。

我有这张桌子:

enter image description here

以下是我从上表中获取行的方式:

var trows = table.rows;

这样我就可以从上面的表中获取所有行,包括标题行。 我的问题是有没有办法从表中获取除标题行之外的所有行?

3 个答案:

答案 0 :(得分:3)

在回答你之前,我想建议如下所示构建表格结构,以便你可以得到你真正需要的结果。

    <table>
      <thead>
       <tr>
          <th>heading</th>
       </tr>
      </thead>
     <tbody id="tableid">
       <tr>
          <td>data1</td>
          <td>data2</td>
       </tr>
     </tbody>
    </table>

然后在你的脚本中,现在你可以得到如下结果

var trows = table.rows;

我希望你对这个答案感到满意,如果不毫不犹豫地发表问题。谢谢

答案 1 :(得分:2)

将所有这些行放在<tbody>中,并将标题放在<thead>内,然后执行

var trows = document.getElementById('tableid').getElementsByTagName('tbody')[0].rows;

答案 2 :(得分:1)

使用.shift

删除数组的第一个值
trows.shift(); // remove first line