jQuery:选择“这个”子元素
我真的对这个特殊的障碍感到疯狂,手头的问题是我有一个非常简单的表格,其中包含行和所有内容。我想要完成的是,当您单击一行时,该脚本应该读取存储在同一行的最后一个 td 中的链接,然后将您引导到那里。
到目前为止我想出的是,
$('tr td').click(
function (){
alert($(this+':parent td:last-child a:first').attr('href'));
}
);
我已经尝试了 100 种不同的方法,我要么得到错误/未定义,要么只得到最后/第一行的结果,中间的行无法按预期工作。
非常感谢所有帮助,
表格如下
I'm really going crazy of this particular obstacle, the issue in hand is that I have a very simple table with rows and all. what I'm trying to accomplish is that when you click on a row then this script is suppose to read the link that's stored in the last td
in the very same row and then direct you there.
What I've come up with so far is this
$('tr td').click(
function (){
alert($(this+':parent td:last-child a:first').attr('href'));
}
);
I've tried 100 of different approaches I either get an error/undefined or I only get the result of the last/first row the rows in-between doesn't work as supposed to.
All help is really appreciated
table looked as following
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你的意思是这样做:
I think you meant to do this:
我认为您需要的是:
此脚本将导致每个表格行在单击时将您重定向到最后一个表格单元格中第一个锚元素引用的位置。
What you need is this I think:
This script will cause each table row, when clicked, redirect you to the location referenced in the first anchor element in the last table cell.
在这种情况下使用
.siblings()
,尽管.parent ().children()
也可以工作,如下所示:但更进一步,不要将
click
处理程序附加到每个单元格,使用.delegate()
为整个表附加一个,如下所示:您可以在这里尝试一下。
Use
.siblings()
in this case, though.parent().children()
also works, like this:Take it one step further though, don't attach a
click
handler to every cell, use.delegate()
to attach one for the entire table, like this:You can try it out here.
http://jsfiddle.net/ppw8z/
http://jsfiddle.net/ppw8z/