我有一個像如何獲得TD的對應日的標識表中的
<table>
<thead>
<tr>
<th id ="myid1">header1</th>
<th id ="myid2">headre "2</th>
<th id ="myid3">header3</th>
</tr>
</thead>
<tr>
<td>v1</td>
<td>v2</td>
<td>v3</td>
</tr>
<tr>
<td>v10</td>
<td>v11</td>
<td>v12</td>
</tr>
<tr>
<td>v20</td>
<td>v21</td>
<td>v22</td>
</tr>
<tr>
<td>v30</td>
<td>v31</td>
<td>v32</td>
</tr>
</table>
的情景可以有成千上萬行的。
我需要獲取該屬性所屬的td的id。例如
。如果我點擊第三行的第三TD ..我應該得到相應的次的ID,這裏是myid3(這裏的硬編碼,但它將爲基於從服務器端的值)
$('tbody td').live('click', function() {
var idOfTh = ??
});
這是不正確的。獲取像這樣的索引:'var tdIndex = $(this).index()'。另外,'.parent('table')'應該是'.closest('table')'('。parent('table')'不會選擇任何元素,因爲'td'的父節點不是'table'),'.find('tr')'應該是'.find('th')' 。 – 2012-08-02 14:20:43
謝謝菲利克斯。但是不會在匹配選擇器中的所有** td元素中獲得td的索引,而不僅僅是該行嗎? – 2012-08-02 14:22:51
不,它獲取其兄弟元素中的索引:*「如果沒有參數傳遞給'.index()'方法,則返回值是一個整數,指示jQuery對象中第一個元素相對於其同級元素的位置。「*(http://api.jquery.com/index/)。 – 2012-08-02 14:23:13