如何使用 JQuery 计算一行中的表格单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8408885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count table cells in a row with JQuery?
提问by aygeta
I have an image gallery with say 39 number of images. Two buttons prev and next and in the middle a counter. Images are inside a table and 6 of images are shown.
When the user presses next button 6 visible images go up and 6 others show.
I want to count number of visible images.
Example 6/39 when next is pressed there should be 12/39. If I do +6 on every click sometimes the last tr has less than 6 images
and when the counter goes +6 it surpasses the number of all images.
So i need to now the index of current tr being showed. Then I need to count the td's of that tr and put this number in the counter and increment.
我有一个图片库,里面有 39 张图片。两个按钮上一个和下一个,中间是一个计数器。图像位于表格内,并显示了 6 个图像。
当用户按下下一步按钮时,6 个可见图像上升,另外 6 个显示。
我想计算可见图像的数量。
示例 6/39 当按下 next 时应该是 12/39。如果我每次点击都做 +6,有时最后一个 tr 的图像少于 6 个,当计数器变为 +6 时,它超过了所有图像的数量。所以我现在需要显示当前 tr 的索引。然后我需要计算该 tr 的 td 并将这个数字放入计数器并递增。
回答by ComputerSaysNo
assuming that your table has id "mytable", you can use
假设您的表的 ID 为“mytable”,您可以使用
$( "#mytable tr td" ).length
but, say you want to access row 2, then you use
但是,假设您要访问第 2 行,然后使用
$( "#mytable tr:nth-child(2) td" ).length
but if you want to count the "td"'s of the visible row, use
但是如果你想计算可见行的“td”,请使用
$( "#mytable tr:visible td" ).length