使表行可单击

Anj*_*apa 2 jquery row html-table click

我有一个在悬停时具有背景颜色的表格行.当用户在背景颜色区域内单击时,它应该抓住行内锚标记的链接并将用户带到那里..我该怎么做?

<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>
Run Code Online (Sandbox Code Playgroud)

如何获取锚标签href值?

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

        });
Run Code Online (Sandbox Code Playgroud)

bar*_*iir 8

好吧首先,如果您仍然使用id,则无需在选择器中指定tr.如果你想要,你应该在没有空格的情况下一起编写,因为tr得到了这个id.

其次,你需要使用thisfind()选择被点击的表格行中的第一个链接并获取它的href属性:

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

以下也有效:

location = $(this).find('a:first').attr( 'href' );
Run Code Online (Sandbox Code Playgroud)

请参阅:Javascript:设置location.href与位置