[英]get the zero based index of tds onclick in table row
Hi have a table that has many rows 嗨,有一个表有很多行
The first row has <th>
s then all the following rows contain <td>
s 第一行包含
<th>
然后所有随后的行包含<td>
If I use the script below and click on the the first row with <td>
s in it I get an alert saying 1. How do I make it say zero with out subtracting one. 如果我使用下面的脚本,然后单击第一行中带有
<td>
的行,则会收到一条警告,说:1。如何使它说零而不减去一。 I thought that using "has(td)" would work? 我以为使用“ has(td)”有效吗?
$('td').click(function(){
var s = $(this).parents('table tr:has(td)').index();
alert(s);
});
<table border="1" width="400px" height="200px">
<tr>
<th></th><th></th><th></th><th></th><th></th>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td> if I click here it must alert 0
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td> if I click here it must alert 1
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td> if I click here it must alert 2
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td> if I click here it must alert 3
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td> if I click here it must alert 4
</tr>
</table>
Pass the a selector to .index()
to filter out the siblings 将选择器传递给
.index()
以过滤出同级
$('td').click(function(){
var s = $(this).parent().index('tr:has(td)');
alert(s);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.