2013-01-22 44 views
2
<table> 
    <tr> 
    <td>data</td> 
    <td>data</td> 
    </tr> 
    <tr class='product_details'> 
    <td>product details</td> 
    <td>product details</td> 
    </tr> 
    <tr class='product_details'> 
    <td>product details</td> 
    <td>product details</td> 
    </tr> 
</table> 

我想计算其类为product_details的表行。我如何用jQuery做到这一点?如何用jquery计算tr元素?

回答

5

您可以使用jQuery的length财产class selector

Live Demo

len = $('.product_details').length //will find any element with class product_details 

如果您有与其他元素相同的类,然后TR然后

len = $('tr.product_details').length //will find only tr with class product_details 

表中发现,使用后代选择器,带表。

len = $('table tr.product_details').length 

如果分配ID表这将是更好,让说的id是table1

len = $('#table1 tr.product_details').length 
+0

0票下来 \t $( “product_details”)。长度将返回其具有product_details类元素的个数。 – Rajiv007

+0

没有类product_details的元素,但我觉得在安全的一面,我们将tr包含在选择器中。我已经更新了我的答案。 – Adil

0
var count = $('table tr.product_details').length;
1
var n = $("table tr.product_details").length; 
alert(n); 
1
$(".product_details").length 

将返回其具有product_details

元素个数
1

尝试t他以下

$('table tr.product_details').length 

$('table tr.product_details').size()