在 jQuery 中有没有一种方法可以判断选定行中有多少个单元格?例如,我通过以下方式遍历行:
$("#statsId > table tbody tr").each ( function() {
var secondCol = $(this).find('td:nth-child(2)').html();
var thirdCol= $(this).find('td:nth-child(3)').text();
并希望检索第二个和第三个单元格值。但是,当我进行此迭代时,当我知道它们不是时,单元格值将返回 null。我想如果有办法验证所选行中的单元格/列的数量,我至少可以验证选择器中有三个单元格。或者我的“secondCol”和“thirdCol”选择器可能是错误的。感谢您的帮助。
最佳答案
你可以用它来测试:
if( $(this).children('td').length >= 3 ) {
...
}
但也可以尝试使用 :eq()
:
var secondCol = $(this).find('td:eq(1)').html();
var thirdCol= $(this).find('td:eq(2)').text();
关于javascript - jQuery - 一行中有多少个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1894560/