jQuery 是否有与 .closest() 等效的向下搜索而不是向上搜索的 DOM 树?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5139375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:36:59  来源:igfitidea点击:

Is there an equivalent to .closest() that searches down the DOM tree instead of up?

jquery

提问by ben

Is there an equivalent to .closest()that searches down the DOM tree instead of up?

是否有与.closest()等效的向下搜索而不是向上搜索的 DOM 树?

采纳答案by Guffa

The closestmethod does actually search down the tree (despite what the documentation says), but I know what you mean. You want one that searches among the children of the element. Depending on how you want to search:

closest方法确实会搜索树(尽管文档中有说明),但我知道您的意思。你想要一个在元素的子元素中搜索的。取决于您要搜索的方式:

$('#Id').children('div');

or

或者

$('#Id').find('div');

回答by gor

You can use find()method. And get the firstelement from resulting set.

您可以使用find()方法。并从结果集中获取第一个元素。

回答by Roy Shoa

Closest is searching UP related to the documentations "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" and as you can see "The .children()method differs from .find()in that .children()only travels a single level down the DOM tree while .find()can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well". I did not found a good solution for closestdown the tree because the good thing about closestis that its stop searching elements when it find the first matched selector and findcontinue searching. So what you can do is to use the .first()filter.

最接近的是搜索与文档相关的 UP“对于集合中的每个元素,通过测试元素本身并遍历其在 DOM 树中的祖先来获取与选择器匹配的第一个元素”,正如您所看到的“该.children()方法不同于.find()因为它.children()只能沿着 DOM 树向下移动一个级别,同时.find()也可以向下遍历多个级别以选择后代元素(孙子等)”。我没有找到一个好的解决方案,closest因为它的好处closest是它在找到第一个匹配的选择器时停止搜索元素并find继续搜索。所以你可以做的是使用.first()过滤器。

$('selector').first();

OR

或者

obj.find('selector').first();

It will find a match of multiple element but will return only the first element like closestbut less performance.

它将找到多个元素的匹配项,但只会返回第一个元素,closest但性能较低。

https://api.jquery.com/first/

https://api.jquery.com/first/

回答by Robert Siemer

No, not with jQuery. But with pure DOM, there is for single elements:

不,不是 jQuery。但是对于纯 DOM,有单个元素:

$($element[0].querySelector('.whatever'))

Pick the first (and only?) element from your selection, run the “DOM .find()” and wrap it up again.

从您的选择中选择第一个(也是唯一的?)元素,运行“DOM .find()”并再次包装它。

Compared to the other answers here, this is the only (short) way to stop wasting CPU cyclesafter finding the first match.—It leaves me, once again, with a bitter aftertaste of the thoughtfulness in jQuery. The DOM feels incomplete, too, no doubt, but at least they have the shortcut version of .querySelectorAll().

与这里的其他答案相比,这是在找到第一个匹配项后停止浪费 CPU 周期的唯一(简短)方法。- 它再次让我回味 jQuery 的深思熟虑。毫无疑问,DOM 也感觉不完整,但至少它们有.querySelectorAll().

回答by Oliver K?tter

$('#Id div:first')does also what you are looking for.

$('#Id div:first')也可以满足您的需求。

Please also note that while closest() only returns one element, find(), children() and also $("#Id div") returns all matching elements, so you must add .first() or :first to reduce the result to the first occurence.

另请注意,虽然最近()只返回一个元素,find(),孩子()和$(“#Id div”)返回所有匹配的元素,所以你必须添加 .first() 或 :first 以减少结果到第一次出现。

回答by NXT

In case somebody needs to search UP, it's easy with .parents()method.

如果有人需要搜索 UP,使用.parents()方法很容易。

You can leave it empty, or specify selectors like .parents("div").

您可以将其留空,或指定选择器,如.parents("div")