«

JavaScript获取表格(table)的行数和列数

时间:2024-1-20 16:30     作者:韩俊     分类: Javascript


如何使用JavaScript获取表格(table)的行数和列数?其实很简单,假设存在如下表格:

<table width="100%" border="1" cellspacing="0" cellpadding="0" id="example_table">
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
</table>

则获取该表格的行数和列数可分别如下代码:

//表格行数
var rows = document.getElementById("example_table").rows.length;

//表格列数
var cells = document.getElementById("example_table").rows.item(0).cells.length;

标签: javascript

热门推荐