简体   繁体   English

父级的 jQuery 父级

[英]jQuery parent of a parent

I am currently trying to find the parent of a parent of an element.我目前正在尝试查找元素的父级的父级。 I have a link being clicked that is in a <td> , and I'd like to get the <tr> object.我在<td>有一个被点击的链接,我想获得<tr>对象。

Why wont "$(this).parent().parent()" work?为什么“$(this).parent().parent()”不起作用? What will?什么会?

Thanks,谢谢,
Brendan布伦丹

Edit: It appears an error in my syntax was throwing the whole thing off.编辑:我的语法中的一个错误似乎把整个事情都搞砸了。 "$(this).parent().parent()" does in fact work, but I wound up going with $(this).closest('tr')" because it seems like the most efficient solution. "$(this).parent().parent()" 确实有效,但我最终选择了 $(this).closest('tr')" 因为它似乎是最有效的解决方案。

The best way would probably be using closest :最好的方法可能是使用closest

$(this).closest('tr');

Check out the documentation :查看文档

Closest works by first looking at the current element to see if it matches the specified expression, if so it just returns the element itself. Closest 首先查看当前元素以查看它是否与指定的表达式匹配,如果匹配则返回元素本身。 If it doesn't match then it will continue to traverse up the document, parent by parent, until an element is found that matches the specified expression.如果不匹配,它将继续向上遍历文档,父级一个父级,直到找到与指定表达式匹配的元素。 If no matching element is found then none will be returned.如果没有找到匹配的元素,则不会返回任何元素。

It should work.它应该工作。 You can also try $(this).parents(tag) , where tag is the tag you want to find.您也可以尝试 $(this).parents(tag) ,其中 tag 是您要查找的标签。

For example:例如:

$(this).parents("tr:first")

Will find the closest tr "up the chain".将找到最接近的 tr “上链”。

That should work... you might try那应该有用……你可以试试

$(this).parents(':eq(1)');

The .parents(selector) says get all ancestors that match the selector .parents(selector) 表示获取与选择器匹配的所有祖先

and the :eq(1) says find the oneth (zero-indexed, so the second) element in the list :eq(1) 表示找到列表中的第一个(零索引,所以是第二个)元素

This snippet has performed for me in the past:这个片段过去曾为我表演过:

$(this).parent().parent(); 

Post some code for us to see if there might be another problem somewhere...发布一些代码让我们看看是否还有其他问题......

也试试

$(this).closest('div.classname').hide();

If you have any sort of id/class for the parent, you can use parents() but that will give you all parents up to the < body > unless you filter() or stop it some other way like如果你有任何类型的父级 id/class,你可以使用 parents() 但这会给你所有的父级到 < body > 除非你 filter() 或以其他方式停止它

$(this).parents('.myClass');

Hope this helps someone :)希望这对某人有所帮助:)

var getParentNode = function(elem, level) { 
    level = level || 1; 
    for (var i = 0; i < level; i++) {
        if (elem != null) {
            elem = elem.parentNode; 
        }
    }
    return elem; 
}

Try wrapping the $(this).parent() into an jQuery object like $($(this).parent()) I often find the need to do this to make sure I have a valid jquery object.尝试将 $(this).parent() 包装到像 $($(this).parent()) 这样的 jQuery 对象中,我经常发现需要这样做以确保我有一个有效的 jquery 对象。 From there you should be able to get a hold of the parents parent, or using the prev() perhaps.从那里你应该能够获得父母的父母,或者使用 prev() 。

.closest() is not always best option specially when you have same element construct. .closest()并不总是最好的选择,特别是当您具有相同的元素构造时。

<div>
    <div>
        <div>
        </div>
    </div>
</div>

You can do parent of a parent and it's very easy:你可以做父母的父母,这很容易:

var parent = $('.myDiv').parent();
var parentParent = $(parent).parent();
var parentParentParent = $(parentParent).parent();

etc.等等。

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

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