Jquery选择<td>标记

Jquery select first parent of <td> tag in HTML table

本文关键字:标记 gt td lt 选择 Jquery      更新时间:2023-09-26

我在应用程序中使用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标签的最佳方式是什么?

使用

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

演示

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

DEMO

在jquery 中使用.siblings()

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

或者如果不止一个同级具有此类.aco

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

将选择与单击的td平行的th并显示其文本。您可以在选定的th而不是.text()上执行不同的功能。