我有一个HTML页面。它包含表格。我想找出一个位于同一位置的特定单元格索引。即 这是我的表,
<table>
<tr>
<td rowspan="2">Label 1</td>
<td rowspan="2">Label 2</td>
<td colspan="2">Label 3</td>
<td>Something else</td>
</tr>
<tr>
<td>Item 1</td>
<td>Item 2</td>
<td>Item 3</td>
</tr>
<tr>
<td> 1</td>
<td> 2</td>
<td> 3</td>
<td> 4</td>
<td> 5</td>
</tr>
</table>
这里我想在item2的相同位置找到下一行td的索引。(即索引为4)。 如何使用jQuery?
答案 0 :(得分:0)
试试这个。
代码段:(已更新)
$(document).on('click', 'button', function () {
var ele = $('table tr:eq(1)'), childEle = ele.children() ;
var len = childEle.length;
for(var i=0; i< len;i++){
var tdText = childEle.eq(i).text();
var itemIndex = ele.children('td:contains("'+tdText+'")').index();
var positionFromLast = len-(itemIndex);
var nxtTrEle = ele.next();
var nxtTrLength = nxtTrEle.children().length;
var ind = nxtTrLength - positionFromLast;
var reqIndex = nxtTrEle.children(':eq('+ind+')').index();
alert(reqIndex);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table>
<tr>
<td rowspan="2">Label 1</td>
<td rowspan="2">Label 2</td>
<td colspan="2">Label 3</td>
<td>Something else</td>
</tr>
<tr>
<td>Item 1</td>
<td>Item 2</td>
<td>Item 3</td>
</tr>
<tr>
<td> 1</td>
<td> 2</td>
<td> 3</td>
<td> 4</td>
<td> 5</td>
</tr>
</table>
<br/>
<button>Get Parallel Index</button>
希望这有帮助!