1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

tableのtrをリンクにしつつ、その中に通常のリンクを置く (Pure.js)

Last updated at Posted at 2021-09-09

data-hrefの中にリンクを置きたいとき、
もしくはボタン、チェックボックスを置きたいときの備忘として残します。

<table>
    <thead>
        <tr>
            <th>リンク先</th>
            <th>通常リンク</th>
        </tr>
    </thead>
    <tbody>
        <tr data-href="https://qiita.com/" tabindex="0" role="link">
            <th>https://qiita.com/</th>
            <td class="object">
                <a href="#aaa">編集する</a>
            </td>
        </tr>
    </tbody>
</table>
  • tabindex="0"でタブ操作を有効にしてます
var td = document.querySelectorAll('tr[data-href] > *:not(.object)');

Array.prototype.forEach.call(td, function (e) {
    e.addEventListener('click', function() {
        window.location = e.parentNode.getAttribute('data-href');
    });
});

デモはこちら

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?