我需要从下一个表中获取id或class,例如,当我点击蓝色方 block 中的任何div时,从红色方 block 的表中获取id或class。
如果我使用这个:
$('div').click(function() {
console.log($(this).next().find('table').attr('id));
});
但是不起作用!
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()
关于jquery - 单击 div 时查找下一个表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15423870/