はい。クリックした要素から、 経由で行に移動すると、またはを使用して関連するクラスで をclosest
見つけることができます(おそらく、ネストされたテーブルがある場合)。td
find
children
children
function clickHandler(e) {
var cell = $(e.target).closest('tr').children('td.foo');
// ...
}
または、常に最初 が必要な場合td
:
var cell = $(e.target).closest('tr').children('td').first();
そのハンドラーはtd
、テーブル内のいずれかに割り当てられている場合tr
、または実際にtbody
またはtable
全体として割り当てられている場合に機能します。
実例| ソース
JavaScript:
jQuery(function($) {
// Put the handler on the tbody
$("#target").click(clickHandler);
function clickHandler(e) {
var td = $(e.target).closest('tr').children('td').first();
display("The text of the first <code>td</code> in that row is: " + td.text());
}
function display(msg) {
$("<p>").html(String(msg)).appendTo(document.body);
}
});
HTML:
<table>
<tbody id="target">
<tr>
<td class="one">This is one in row 0</td>
<td class="two">This is two in row 0</td>
<td class="three">This is three in row 0</td>
</tr>
<tr>
<td class="one">This is one in row 1</td>
<td class="two">This is two in row 1</td>
<td class="three">This is three in row 1</td>
</tr>
<tr>
<td class="one">This is one in row 2</td>
<td class="two">This is two in row 2</td>
<td class="three">This is three in row 2</td>
</tr>
</tbody>
</table>