3
4

More than 3 years have passed since last update.

tableの行全体(tr)をリンクにする(jQuery)

Last updated at Posted at 2020-08-27

trタグ全体や、tdタグの塊をaタグで囲っても、行全体をリンクにすることはできないようです。
jqueryを使って実装したので、サンプルを記録しておきます。

<table>
  <tr data-href="/hoge/">
    <td>foo</td>  
    <td>bar</td>
    <td>baz</td>          
  </tr>
</table>

<script>
  $('tr[data-href]').click(function (e) {
      if (!$(e.target).is('a')) {
        window.location = $(e.target).data('href');
      };
  });
</script>

<style>
  tr:hover {
    background: gray;
  }
</style>

See the Pen JjXNJVg by d0ne1s (@d0ne1s) on CodePen.

3
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4