Jquery在HTML表中选择标记的第一个父级

时间:2014-06-25 12:15:44

标签: javascript jquery html

我在我的应用程序中使用Jquery V1.11.1。我有一个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的TH标签。

我认为这会做到,但它没有。 $(this).parent('th').text();

在这种情况下使用Jquery选择TH标签的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

使用

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

DEMO

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

DEMO

答案 1 :(得分:1)

在jquery中使用.siblings()

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

答案 2 :(得分:0)

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

或者如果有多个兄弟姐妹有此课程.aco

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

将选择与所点击的th平行的td并显示其文字。您可以对选定的th而不是.text()执行不同的功能。