简体   繁体   English

jQuery - parent()vs nearest()

[英]jQuery - parent() vs closest()

Say I have the following markup: 说我有以下标记:

<div class="parent">
    <div class="child">
        <div class="grand-child">

And I want to get .parent starting from .grand-child . 我想.parent从开始.grand-child I can do either of the following two: 我可以做以下两种情况之一:

$('.grand-child').parent().parent();
$('.grand-child').closest('.parent');

Overall, closest() feels like a nicer approach since: 总的来说, closest()感觉就像一个更好的方法,因为:

  1. Only one function 只有一个功能
  2. It is irrelevant to other divs between the .grand-child and .parent 它与.grand-child.parent之间的其他div无关

Specifically, due to advantage of #2 above, if I were to change my markup to 具体来说,由于上面#2的优点,如果我要将我的标记更改为

<div class="parent">
    <div class="child">
        <div class="nanny">
            <div class="grand-child">

Then I need to add an extra parent() but closest() still functions fine. 然后我需要添加一个额外的parent()closest()仍然可以正常运行。

So is there a reason why you would ever choose parent() or closest() ? 那么你有没有理由选择parent()closest()

you should use $('.grand-child').closest('.parent'); 你应该使用$('.grand-child').closest('.parent'); because .parent().parent() is strongly based on your html structure if in future you add another div inside one of these then you will get wrong element using parent().parent() 因为.parent().parent()强烈基于你的html结构,如果以后你在其中一个中添加另一个div,那么你将使用parent().parent()得到错误的元素.parent().parent() parent().parent()

Suppose you have html like: 假设你有像这样的html:

<div class="parent">
    <div class="child">
        <div class="grand-child-container">
             <div class="grand-child">

now what will happen if you use .parent().parent() it will give you wrong element, so recommended way is to use closest() as it is much better. 如果你用现在会发生什么.parent().parent()它会给你一个错误的元素,所以推荐的方法是使用closest()因为它是要好得多。

According to docs: 根据文件:

parent() gets the parent of each element in the current set of matched elements, optionally filtered by a selector. parent()获取当前匹配元素集中每个元素的父元素,可选择由选择器过滤。

closest() For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. nearest()对于集合中的每个元素,通过测试元素本身并遍历DOM树中的祖先来获取与选择器匹配的第一个元素。

Differences: 区别:

if you scroll little down you can see differences between these two by official jquery site. 如果你向下滚动一下,你可以通过官方的jquery网站看到这两者之间的差异。

There are also some more good answers available on differences between these two: 关于这两者之间的差异,还有一些更好的答案:

parent vs closest 父母与最近

Difference between jQuery parent(), parents() and closest() functions jQuery parent(),parents()和nearest()函数之间的区别

Performance: 性能:

Also performance wise closest() is better than parent() , you can check Benchmark between closest() and parent() 性能明智的closest()也比parent() ,你可以检查最近()和父()之间的基准

If you look at the code, .parent() is basically just a wrapper for the DOM call .parentNode . 如果查看代码, .parent()基本上只是DOM调用.parentNode的包装器。 It's very quick and efficient, since it's essentially a single call to a browser built-in. 它非常快速有效,因为它基本上是对内置浏览器的单次调用。

https://github.com/jquery/jquery/blob/master/src/traversing.js#L133 https://github.com/jquery/jquery/blob/master/src/traversing.js#L133

.closest(selector) is indeed much safer, because it guarantees you don't loop up past, or don't loop too briefly and stop before, you arrive at the intended parent - regardless of the actual shape of the DOM. .closest(selector)确实更安全,因为它保证你不会循环过去,或者不要过于简单地循环并在之前停止,你到达预期的父级 - 无论DOM的实际形状如何。 But it's obviously also much more expensive. 但它显然也要贵得多。 Look at the code: 看看代码:

https://github.com/jquery/jquery/blob/master/src/traversing.js#L63 https://github.com/jquery/jquery/blob/master/src/traversing.js#L63

That it's a loop at all is inherently pricier. 这根本就是一个循环,本质上更昂贵。 It's also doing a lot more checks each iteration. 它每次迭代都会做更多的检查。 In a very deep DOM, if you're for example using .closest to ask "Is this tag a child of X?" 在一个非常深的DOM中,如果您使用.closest来询问“这个标签是X的孩子吗?” it will loop up the entire DOM tree to the body tag with no other bound. 它将整个DOM树循环到body标签而没有其他绑定。 If you're 1000 tags deep that's 1000 pointless loops to find it. 如果你有1000个标签深度,那就是1000个无意义的循环来找到它。 The same can occur if you're simply wrong about where the tag you're calling this is placed. 如果您对要调用此标记的位置的位置完全错误,则会出现同样的情况。

So, .parent() is highly efficient for a DOM where you're very certain about the structure. 因此, .parent()对于您非常确定结构的DOM非常有效。 .closest(selector) is for less-trusted DOMs, although somewhat dangerous if you have no idea what the DOM looks like. .closest(selector)用于不太可信的DOM,但如果你不知道DOM是什么样的话会有些危险。

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

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