有没有办法同时执行.filter和.find

Is there a way to perform .filter and .find at the same time?

本文关键字:filter find 执行 有没有      更新时间:2023-09-26

我正在处理模板,并在响应事件时查找某些元素以供将来更新。但是,我希望能够用类、data-*属性或其他东西标记这些元素,而不必事先知道标记。

有没有办法同时执行.filter()和.find()搜索?

我目前的解决方案是使用.add()操作来组合这两个结果,但这似乎有点复杂。

elements.filter(selector).add(elements.find(selector))

用于此目的的简单扩展:

jQuery.fn.findIn = function(selector){
  return this.filter(selector).add(this.find(selector));
}

然后简单地调用.findIn('.your-selector')

目前正在接受一个更好的名字的建议