使用 jquery 获取 tr 的第二个 td
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40214003/
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
Get second td of tr using jquery
提问by Syed
I'm trying to get td values of tr.. but unfortunately it's not working.. may be there is something wrong
我正在尝试获取 tr 的 td 值 .. 但不幸的是它不起作用.. 可能有什么问题
My html looks like
我的 html 看起来像
<tr class="dname">
<td>abc</td>
<td>value here</td>
</tr>
My jquery code is
我的 jquery 代码是
jQuery(".dname").find("tr td:eq(1)").val();
What's wrong in this ?
这有什么问题?
回答by samjudson
jQuery find()
method returns the descendants of the selected element. You are already selecting the <tr>
with a class of dname
and then trying to find a descendant which is also a <tr>
.
jQueryfind()
方法返回所选元素的后代。您已经在选择<tr>
类为 of 的dname
,然后尝试查找也是<tr>
.
The following should work:
以下应该工作:
jQuery(".dname").find("td:eq(1)").text();
Edit: text()
instead of val()
as @freedomn-m pointed out
编辑:text()
而不是val()
@freedomn-m 指出
回答by Tarun Khurana
Replace n with child no.
用孩子编号替换 n。
$(".dname td:nth-child(n)").text();
For e.g. for 2nd child
例如对于第二个孩子
$(".dname td:nth-child(2)").text();
回答by Darragh Enright
回答by SilentCoder
Because that <td>
is the last child of your <tr>
you can access it like below,
因为那<td>
是你的最后一个孩子,<tr>
你可以像下面那样访问它,
jQuery(".dname > td:last-child").text();
jQuery(".dname > td:last-child").text();