老二的老二——Jquery
我想知道如何获得孩子的孩子的价值。我有这个...
<table id="table4">
<tr>
<td>Id:</td>
<td>Name:</td>
</tr>
<tr>
<td>1515</td>
<td>Thiago</td>
</tr>
</table>
在 Jquery 中,我想做这样的事情...
var id = $("#table4:nth-child(2) tr:nth-child(1)").val();
我怎样才能完成这项工作?第一个 和第二个
。我知道我可以使用“class”和“id”来使其更容易,但在这种情况下,由于其他原因我不能这样做。
附言。抱歉英语不好,我是这门语言的菜鸟。
谢谢。
I wanna know how can I get a value of a child of a child. I have this...
<table id="table4">
<tr>
<td>Id:</td>
<td>Name:</td>
</tr>
<tr>
<td>1515</td>
<td>Thiago</td>
</tr>
</table>
In Jquery, I wanna do something like this...
var id = $("#table4:nth-child(2) tr:nth-child(1)").val();
How can I do this work? The first <td>
of the second <tr>
. I know that I can use "class" and "id" to make it easier, but in this case I can't do that for others reasons.
PS. Sorry about the bad English, I'm a noob in this language.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样:
您可以在这里测试。
:nth-child
选择器将应用于子元素,例如第一个表示“athat is a secondary child它的父版本”,而不是您尝试的版本,这意味着“此
的第二个子”。
另外,使用
.text()
获取元素文本,而不是.val()
用于输入类型元素。Like this:
You can test it out here. The
:nth-child
selector applies to the child, for instance the first is saying "a<tr>
that is a second child of its parent", rather than your attempted version, which would mean "the second child of this<tr>
".Also, use
.text()
to get an elements text, rather than.val()
which is for input type elements.这不是一个完全的 jQuery 解决方案,但我想指出你可以这样做:
或者如果你想避免任何 HTML 标记,你可以这样做:
This isn't a fully jQuery solution, but just thought I'd point out that you could do this:
Or if there will be any HTML markup you want to avoid, you could do this: