如何从表格单元格(td)获取相应的表格标题(th)? [英] How can I get the corresponding table header (th) from a table cell (td)?
本文介绍了如何从表格单元格(td)获取相应的表格标题(th)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出下表,如何为每个td元素获取相应的表头?
Given the following table, how would I get the corresponding table header for each td element?
<table>
<thead>
<tr>
<th id="name">Name</th>
<th id="address">Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>Bob</td>
<td>1 High Street</td>
</tr>
</tbody>
</table>
鉴于我目前已经可以使用任何td
元素,如何找到相应的th
元素?
Given that I currently have any of the td
elements available to me already, how could I find the corresponding th
element?
var $td = IveGotThisCovered();
var $th = GetTableHeader($td);
推荐答案
var $th = $td.closest('tbody').prev('thead').find('> tr > th:eq(' + $td.index() + ')');
或者稍微简化
var $th = $td.closest('table').find('th').eq($td.index());
这篇关于如何从表格单元格(td)获取相应的表格标题(th)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文