如果单击某行的子元素,如何获取该行的 id 属性?
如何返回链接所在行的 id 属性?我尝试过不同的变体,但我能得到的最好的是“未定义”...
html 看起来像这样:
<tr id="30">
<td>Parker</td>
<td><a href="#" id="id783900" class="section-id">Select</a></td>
</tr>
我尝试过的事情之一是这样的:
$("a.section-id").click(function() {
var parenttr = $(this).parent().parent().attr("id");
alert(parenttr);
});
我也尝试过这个:
$("a.section-id").click(function() {
var parenttr = $(this).parent('tr').attr("id");
alert(parenttr);
});
How do I return the id attribute of the row in which a link resides? I've tried different variations, but the best I've been able to get is "undefined"...
The html looks like this:
<tr id="30">
<td>Parker</td>
<td><a href="#" id="id783900" class="section-id">Select</a></td>
</tr>
One of the things I tried is this:
$("a.section-id").click(function() {
var parenttr = $(this).parent().parent().attr("id");
alert(parenttr);
});
I also tried this:
$("a.section-id").click(function() {
var parenttr = $(this).parent('tr').attr("id");
alert(parenttr);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
Try:
Parent() 仅检查直接父级。要遍历树,请使用parents()。
编辑:
树面是正确的。对于此用例,请使用closest()。 parents() 做了一些你不需要的额外工作来解决这个问题。
parent() only checks the immediate parent. To traverse up the tree, use parents().
EDIT:
treeface is correct. Use closest() for this use case. parents() does extra work that you don't need for this problem.