jQuery:如何在元素数组上结合查找和过滤?

发布于 2024-12-20 02:49:10 字数 580 浏览 2 评论 0原文

我有一个元素数组 myRoots。我想找到数组中与某个选择器匹配的元素或数组中某个元素的后代中的所有元素(假设是 div)。

您可以将其视为 myRoots.find('div')myRoots.filter('div') 的组合。我想我可以将其写为 $.merge(myRoots.find('div'), myRoots.filter('div')),但想要 避免重复我自己myRoots 和选择器被重复)。有更好的办法吗?

在 XPath 中,这对应于 descendant-or-self 轴,然后您将编写此 $myRoots/descendant-or-self::div

I have an array of elements myRoots. I want to find all the elements from the array or descendant of an element in the array that matches a certain selector (let's say that are div).

You can look at it as the combination of myRoots.find('div') and myRoots.filter('div'). And I imagine I could write this as $.merge(myRoots.find('div'), myRoots.filter('div')), but would like to avoid repeating myself (myRoots and the selector are repeated). Is there a better way?

In XPath, this corresponds to the descendant-or-self axis, and you'd write this $myRoots/descendant-or-self::div.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖微凉心微凉 2024-12-27 02:49:10

编辑

尝试使用:
myRoots.find("div").andSelf().filter("div");


你可以这样做:

myRoots.find("div").andSelf();

Edit

Try using:
myRoots.find("div").andSelf().filter("div");


You could do:

myRoots.find("div").andSelf();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文