2014-06-25 186 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()