Skip to content

Instantly share code, notes, and snippets.

@alt-karate
Created May 27, 2021 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alt-karate/44a7b51fcd14df468719b1c0c91ca25f to your computer and use it in GitHub Desktop.
Save alt-karate/44a7b51fcd14df468719b1c0c91ca25f to your computer and use it in GitHub Desktop.
jQuery(function($) {
//data-hrefの属性を持つtrを選択しclassにclickableを付加
$('tr[data-href]').addClass('clickable')
//クリックイベント
.click(function(e) {
//e.targetはクリックした要素自体、それがa要素以外であれば
if(!$(e.target).is('a')){
//その要素の先祖要素で一番近いtrの
//data-href属性の値に書かれているURLに遷移する
window.location = $(e.target).closest('tr').data('href');}
});
});
<tr data-href="http://ja.wikipedia.org/wiki/%E6%9D%B1%E4%BA%AC">
<td>東京</td>
<td>とうきょう</td>
<td>Tokyo</td>
<td>日本の事実上の首都がある</td>
<td><a href="#">ダミーリンク</a></td>
</tr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment