使用jquery计算表中的行数 [英] Counting the number of rows in a table NOT using jquery

查看:111
本文介绍了使用jquery计算表中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<head>
<script language="javascript">
// must have the onload handler
onload = function countRows(){
    var rows = document.getElementById('myTableId').getElementsByTagName('tbody')[0].rows.length;
    alert( rows);
// outputs 3
}

</script>
</head>

<body>
    <table id="myTableId">
        <tbody>
            <tr><td></td><td><input onclick="doA_Function_That_Includes_CountRows()" />
            </td></tr>
            <tr><td></td><td></td></tr>
            <tr><td></td><td></td></tr>
        </tbody>
    </table>
</body>
</html>


推荐答案

试试这个:

var rows = document.getElementById('myTableId').getElementsByTagName('tbody')[0].getElementsByTagName('tr').length;

http:// jsfiddle.net/y8URn/

它会计算< tr> 的数量< tbody> ,这又是表格中的行数。

It will count the number of <tr>s in the <tbody>, which in turn will be the number of rows in the table.

请注意它不会仅计算表格表格中的所有行。如果您有一个< thead> 或一个< tfoot> ,或者甚至是tbody之外的一行,那么不计算在内。

Do note that it will NOT count all of the rows in the table only in the table body. If you have a <thead> or a <tfoot> or even a row outside of the tbody, it will not be counted.

这篇关于使用jquery计算表中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆