jquery - 单击 div 时查找下一个表格

标签 jquery html-table jquery-selectors

我需要从下一个表中获取id或class,例如,当我点击蓝色方 block 中的任何div时,从红色方 block 的表中获取id或class。

如果我使用这个:

$('div').click(function() {
    console.log($(this).next().find('table').attr('id));
});

但是不起作用! enter image description here

HTML 代码

<div id="statusServ">
    Status ::
    <div id="todosTicket" class="colorstatus">Todos</div>
    <div id="pendiente" class="colorpendiente colorstatus">Pendiente</div>
    <div id="atencion" class="coloratencion  colorstatus">En Atención</div>
    <div id="concluido" class="colorconcluido colorstatus">Concluido</div>
    <div id="cerrado" class="colorcerrado colorstatus">Cerrado</div> | Acciones ::
</div>
<i>Para visualizar los detalles del ticket, presione sobre la fila deseada.</i>
<table cellspacing="1" id="rBuscarServicio" class="tableResultsSearch tSeguimiento" name="rBuscarServicio">
    <thead>
        <tr>
            <th>#TKMA</th>
            <th>CLIENTE</th>
            <th>EMPRESA</th>
            <th>ING. ASIGNADO</th>
            <th>FECHA CREACIÓN</th>
            <th>OBSERVACIONES</th>
            <th style="display:none;">STATUS</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

最佳答案

next()只会让您到达下一个 sibling ,在本例中是下一个蓝色 <div/> .

你必须爬到树上<div/>这是 <div/> 的同级包含<table/> .

在您的示例中,以下内容应该有效。

Fiddle

$('#statusServ').children().click(function(){
    console.log(
        $(this)             // div that was clicked
            .parent().parent()       // #statusServ
            .find('table')         // #rBuscarServicio_wrapper
            .prop('id')     // 'rBuscarServicio'
    );
});

注意:

一般最好使用prop()而不是 attr()

prop() vs attr()

关于jquery - 单击 div 时查找下一个表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15423870/

相关文章:

html - 如何删除 HTML 表格中行之间的分隔?

jquery - 使用jquery在点击时显示和隐藏许多div

javascript - jQuery 检索选择选项值

javascript - Parsley.js 2.0.2 要求不起作用,但其他验证有效

php - 使用 jQuery Ajax 发送按钮值

Angularjs。 ng-switch 在表格内不起作用

javascript - 如何使用 Javascript 从带有嵌套列表的 JSON 动态创建表?

javascript - 如何使用 jQuery 选择空输入(值 ="")

javascript - 创建新的待办事项代码生成错误

javascript - 页面内容顶部的预加载器