简体   繁体   English

jQuery遍历不适用于.closest()、. parent()。parent()等

[英]Jquery traversal not working with .closest(), .parent().parent() etc

I have an annoying problem whereby I can only get the top two parents of an element and it will not go beyond that. 我有一个令人烦恼的问题,即我只能获得一个元素的前两个父元素,而且不会超出这个范围。 .closest() does not work either. .closest()也不起作用。 When I look at the hierachy it should work according to the specifications outlined in the documentation. 当我查看层次结构时,它应该根据文档中概述的规范工作。

Here is what I have: 这是我所拥有的:

<div id="telephonezone" class="bbox">
<table id="phones" cellspacing="0" width="100%">
<thead>
<tr><th width="70">Type</th><th>Phone number</th><th width="30"></th>
</tr></thead>
<tbody><tr style="background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial;">
<td class="primaryset">N/A</td>
<td class="primaryset">0208 989 8183</td>
<td class="primaryset">
    <img data-id="0" data-dbid="1126" src="images/dandy-color/cancel.png" title="Delete."></td>
</tr>
</tbody>
</table>
</div>

My Jquery: 我的Jquery:

$('.bbox tr td img').live('click', function(){
id = $(this).attr('data-dbid') ;
cont = $(this).parent().parent().html() ;
alert(cont);
})

cont = $(this).parent().parent().html() ; cont = $(this).parent()。parent()。html(); gets me the contents of the < tr >, but if I try going higher than that I just get undefined. 为我提供了<tr>的内容,但是如果我尝试高于该值,我将变得不确定。 Same results with prevAll, prev, closest or any combination of them. prevAll,prev,closest或它们的任意组合的结果相同。

How do I select the ID of the div at line one when I click on that image inside the td? 在td中单击该图像时,如何在第一行选择div的ID?


  html += '><img data-id="'+i+'" data-dbid="'+v[4]+'" src="images/dandy-color/cancel.png" title="Delete." /></td>' ;

尝试指定您要在所有祖先中找到第一个div父对象。

$(this).parents("div:first").attr("id");

Probably you are trying to access deleted element parents. 可能您正在尝试访问已删除的元素父级。 That's why traversal not working. 这就是为什么遍历不起作用的原因。

<a href="#" onclick="$(this).closest('tr').remove(); console.log('this should be null:' + $(this).closest('div'))">

In this example you can not access 'tr's parents since you have removed it from page. 在此示例中,您无法访问'tr'的父母,因为您已将其从页面中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM