<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 的 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
关于jquery - 如何用jquery统计tr元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14454606/