0
喜有,有許多行得到TDS的從零開始的索引的onclick在錶行
第一行有<th>
當時的所有下列各行包含一個表<td>
小號
如果我使用下面的腳本並點擊第一行<td>
s在其中我得到一個警告說:1.我怎麼說它與零減去一。我認爲使用「有(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>
錯誤標記。你寫的文字超出''標籤 – diEcho 2012-08-08 07:09:33
當然'$(this).parent()。index() - 1'比任何帶有':has'或':contains'的選擇器或任何其他解決方案。我發現它也更容易閱讀。 (但是你的代碼不起作用,因爲沒有參數的'.index()'返回元素索引與DOM中的同胞相關,而不是與jQuery對象中的其他元素有關。) – nnnnnn 2012-08-08 07:23:22