查找某个单元格或td的colSpan标头是Spans [英] Finding a colSpan Header for one of the cells or td's is Spans
问题描述
我有跨多个列的多个列标题,我想找到这些列的正确标题编号/计数.
I have multiple column headers that spans of multiple column and I would like to find the correct header number/count for the columns.
我想输入一个列号并获取标题:示例第5列是RDataTest6 $('td.eq(5)),其标题是HTest3 $('th.eq(2))
I want to enter a column number and get the header: example column 5 is RDataTest6 $('td.eq(5)) and its header is HTest3 $('th.eq(2))
示例:
<table>
<tr>
<th>HTest1</th>
<th colSpan="2">HTest2</th>
<th colSpan="4">HTest3</th>
<th colSpan="2">HTest4</th>
</tr>
<tr>
<td>RDataTest1</td>
<td>RDataTest2</td>
<td>RDataTest3</td>
<td>RDataTest4</td>
<td>RDataTest5</td>
<td>RDataTest6</td>
<td>RDataTest7</td>
<td>RDataTest8</td>
<td>RDataTest9</td>
</tr>
</table>
推荐答案
您应该实现一个保存标头位置的简单数组,见下文,
You should implement a simple array that hold the header location, see below,
var thLocator = [], colCount = 1;
$table.find('tr:first th').each(function () {
for (var i = 0; i < this.colSpan; i++) {
thLocator.push(colCount);
}
colCount++;
});
$table.find('td').click(function () {
alert(thLocator[$(this).index()]);
});
然后您可以随时获取td列的位置.请参见 DEMO ->单击任何TD以确定其col头位置.
And then anytime You can get the location of a td column. See DEMO -> Click on any TD to identify its col head position.
我不确定您要哪个计数,所以我记下了所有列数.参见 演示
I am not sure which count you want so I wrote down all col count. See DEMO
$(function() {
var $table = $('table');
alert('Table Header Count ' + $table.find('tr:first th').length);
var thCount = 0;
$table.find('tr:first th').each(function () {
thCount += this.colSpan;
});
alert('Computed TH Count ' + thCount );
alert('Table TD Col Count ' + $table.find('tr:eq(1) td').length);
});
这篇关于查找某个单元格或td的colSpan标头是Spans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!