jQuery:如何在元素数组上结合查找和过滤?
我有一个元素数组 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑
尝试使用:
myRoots.find("div").andSelf().filter("div");
你可以这样做:
Edit
Try using:
myRoots.find("div").andSelf().filter("div");
You could do: