使表格行可点击

2024-04-29

我有一个表格行,悬停时有背景颜色。当用户在背景颜色区域内单击时,它应该抓取行内锚标记的链接并将用户带到那里。我该如何做到这一点?

<tr id="ClickableRow">
    <td>
<a href="http://somesite.com">Go Here</a>
<p> To find about all the interestng animals found in this tourist attractions including 
zebra, giraffe.....
....
</p>
    </td>
</tr>

如何获取锚点选项卡的 href 值?

 $('tr #ClickableRow').click(function () {
         window.location.href=Anchor tag's href value

        });

好的,首先,如果您使用 id,则无需在选择器中指定 tr。 如果你愿意的话,你应该把它们写在一起,不要有空格,因为 tr 得到了那个 id。

其次,你需要使用this and find()选择单击的表行内的第一个链接并获取它href属性:

$('tr#ClickableRow').click(function () {
  var url = $(this).find('a:first').attr('href');
  window.location.href = url;
});

以下也适用:

location = $(this).find('a:first').attr( 'href' );

See: Javascript:设置 location.href 与 location https://stackoverflow.com/q/2383401/59087

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使表格行可点击 的相关文章