LoginSignup
8
7

More than 5 years have passed since last update.

jQueryを使用してTableタグでクリックした箇所の行番号を取得する

Last updated at Posted at 2016-07-31

以下のコードを書いてみた。
任意の列と行を取得できるのであとはIndexでぐるぐる回すなりすれば好きな情報を取得できるのでは。

<html>
<head>
  <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
</head>
<body>
<table id="table">
 <tr>
  <td>A</td>
  <td>B</td>
  <td>C</td>
 </tr>
 <tr>
  <td>D</td>
  <td>E</td>
  <td>F</td>
 </tr>
 <tr>
  <td>G</td>
  <td>H</td>
  <td>I</td>
 </tr>
</table>
<script>
  $("#table td").bind('click', function(){
    $tag_td = $(this)[0];
    $tag_tr = $(this).parent()[0];
    console.log("%s列, %s行", $tag_td.cellIndex, $tag_tr.rowIndex);
  });
</script>
</body>
</html>

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