jquery - 如何用jquery统计tr元素?

标签 jquery

<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/

相关文章:

javascript - 为什么在我的 Bootstrap Tour 过程中所选元素并不总是弹出?

javascript - 如何在 svg 中制作色带的垂直波浪动画?

javascript - 无法在全屏模态中使图像元素全屏

javascript - 使用jquery和php更新img

javascript - following a function with (jQuery, window, document) 是什么意思?意思?

javascript - 我无法在输入字段上应用颜色选择器 js(jscolor.js)

javascript - 在 jquery 中滚动翻译

javascript - 代码仅适用于 jsfiddle

javascript - 当需要使用 'this' 访问对象的函数时,$.each 中的 'this'

javascript - 我如何使用 Jquery Validate 来验证密码而不是要求它?