jQuery - 选择给定行的第一个单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1425937/
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
jQuery - Select first cell of a given row?
提问by Dan
I have a table with images in one column. When I click the image, would like to get the text value of the first column in that row.
我有一个表格,其中一列包含图像。当我单击图像时,想获取该行中第一列的文本值。
I can get the whole row with this:
我可以用这个得到整行:
var a = $(this).parents('tr').text();
However, I cannot isolate the first cell of the row.
但是,我无法隔离该行的第一个单元格。
I've tried
我试过了
var a = $(this).parents('tr td:first').text();
But that just returns the first cell of the entire table.
但这只是返回整个表格的第一个单元格。
Can anyone help me out?
谁能帮我吗?
Thanks.
谢谢。
回答by Daniel Elliott
How about?
怎么样?
var a = $('td:first', $(this).parents('tr')).text();
回答by dcharles
Here's another option:
这是另一种选择:
var a = $(this).closest('tr').find('td:first').text();