0

尝试使用 first-child 或 nth-child 获取连续的第一个单元格,但语法并不好。这是我正在尝试的,但它不起作用:

....
sections[key][2][sections[key][2].length-1] //Table Row
$(sections[key][2][sections[key][2].length-1]+":first-child") //Should be first cell of row
....

相反,它返回整个文档...

4

3 回答 3

1

您的选择器:

$(sections[key][2][sections[key][2].length-1]+":first-child")

正在尝试将 JavaScript 对象与字符串连接起来,这将导致(类似的)[object HTMLTableRowElement]:first-child显然不会产生 jQuery 对象或 DOM 节点。

鉴于(你说)$(sections[key][2][sections[key][2].length-1]tr元素(尽管你为什么在 jQuery 选择器中使用该符号对我来说是个谜),我建议:

$(sections[key][2][sections[key][2].length-1]).find('td:first-child');

JS 小提琴演示

注意使用td:first-child类,以防止选择所有后续元素的第一个孩子。

但是,要获取所有表格行的第一个单元格(使用 jQuery 更易于理解的符号),我建议:

$('table tr td:first-child')

JS 小提琴演示

或者,用于:nth-child()获取任意编号td

$('table tr td:nth-child(2)')

JS 小提琴演示

于 2013-09-11T06:28:59.677 回答
0

如果您只想返回第一个孩子,为什么不这样做:

sections[key][2][sections[key][2].length-1].firstChild 

请记住,通过 jQuery 选择您可以通过 DOM 轻松获得的内容只会无缘无故地减慢您的代码速度。

于 2013-09-11T06:20:52.883 回答
0

应该:

$(sections[key][2][sections[key][2].length-1]+" td:first-child")
于 2013-09-11T06:22:17.907 回答