2014-06-25 115 views
0

我在我的應用程序中使用Jquery V1.11.1。我有一個HTML表看起來像這樣:jquery在HTML表中選擇​​標記的第一個父標記

<table id="permissions"> 
    <tr> 
     <td></td> 
     <th>Administrators</th> 
     <th>Moderators</th> 
    </tr> 
    <tr> 
     <th class="aco">controllers/users/display</th> 
     <td class="permission">Allowed</td> 
     <td class="permission">Denied</td> 
    </tr> 
</table> 

當你點擊「允許」或「拒絕」我想選擇個標籤含有ACO。

我認爲這樣做,但它does not。 $(this).parent('th').text();

在這種情況下使用Jquery選擇TH標籤的最佳方式是什麼?

回答

1

使用

$(this).closest('tr').find('th.aco').text(); 

DEMO

$(this).siblings('th.aco').text(); 

DEMO

+1

謝謝!這對我有效:) – user3772044

1

使用.siblings() jQuery中

$(this).siblings('th.aco').text(); 
+0

@AnoopJoshi在這個答案中我看不到'parent()'。 – Fr0zenFyr

+0

@ Fr0zenFyr其實他編輯了答案。他的初步答案是類似於$(this).closest(「th」) –

+0

Ohh ..我沒有看到編輯的歷史記錄,所以我假設你想在OP問題上發表此評論。我同意,如果一篇文章在幾秒鐘內被編輯,歷史不會被維護(我不知道爲什麼)。 – Fr0zenFyr

0
$('.permission').on('click', function(){ 
    $(this).siblings('.aco').text(); 
}) 

,或者一個以上的兄弟姐妹擁有該類.aco

$('.permission').on('click', function(){ 
    $(this).siblings('th.aco').text(); 
}) 

將選擇th平行於點擊td並顯示它的文本。您可以在選定的th上執行不同的功能,而不是.text()