【问题标题】:How to get data from the 5th cell in a html table row using jQuery如何使用jQuery从html表格行中的第5个单元格获取数据
【发布时间】:2011-05-16 07:52:16
【问题描述】:
我的代码可以正常工作:
var myValue = $(this).parents('tr:first').find('td:first').text();
是否有这样的东西可以工作,因为下面的代码“不工作”:
var myValue2 = $(this).parents('tr:first').find('td:fifth').text();
如您所见,我正在尝试获取该行中的第 5 列。
【问题讨论】:
标签:
jquery
html
html-table
【解决方案1】:
使用:eq:
var myValue2 = $(this).parents('tr:first').find('td:eq(4)').text();
如果$(this) 指的是与您尝试选择的单元格在同一行中的元素,您可以使用closest 稍微缩短它:
var myValue2 = $(this).closest('tr').find('td:eq(4)').text();
【解决方案2】:
在 jQuery 中,:first 是:eq(0) 的快捷方式;没有名为:fifth 的伪类。您也许可以执行以下操作:
var myValue2 = $(this).parents('tr:first').find('td:nth-child(5)').text();
我认为你可以将两者结合起来:
var myValue3 = $(this).parents('tr:first td:nth-child(5)').text();