如何使用jQuery计算表格单元格内的表格

时间:2012-07-21 05:14:13

标签: jquery html count html-table

我有一些嵌套在单元格表中的表,我想用jQuery计算该单元格中有多少个表,这可能吗?

<table>
<thead>
    <tr>
        <th>name</th>
        <th>age</th>
        <th>address</th>
        <th>birthday</th>
        <th>hoby</th>
        <th>country</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td colspan="6">
            <table class="groupbycountry" id="spain">
                <thead> a row of col title  </thead>
                <tbody> rows of data</tbody>
            </table>
            <table class="groupbycountry" id="italy">
                <thead>a row of col title</thead>
                <tbody>rows of data </tbody>
            </table>
            //and many more table of country according the user input 
        </td>
    </tr>
</tbody>

<tfoot>
    <tr>
        <td colspan="6">
            <table name="inputdata">
                <form name="myprofile">
                    //the input tag
                </form>
            </table>
        </td>
    </tr>
</tfoot>
</table>

是否可以计算该父表中的表class=groupbycountry

2 个答案:

答案 0 :(得分:1)

您可以使用jQuery length属性返回所选元素的数量,请尝试以下操作:

var len = $('table:eq(0) .groupbycountry').length

DEMO

答案 1 :(得分:1)

你可以做 - &gt;

var tCount = $('.groupbycountry').filter('table').length;
$('#tables').text('There are '+tCount+' tables.');

Working jsFiddle