【发布时间】:2013-10-14 22:51:43
【问题描述】:
假设我编写了一个 DOM sn-p:
var dom = $("
<div id='root' class='abc'>
<div id='child1' class='xxx'>no-match</div>
<div id='child2' class='abc'>match!</div>
<div id='child3' class='xxx'>no-match</div>
</div>
");
我想用“.abc”检索所有元素(即#root 和#child2)
dom.find('.abc') 将返回所有匹配的子元素(即仅 #child2)
dom.filter('.abc') 将返回所有匹配的根元素(即仅#root)
我该怎么做:
- 拨打一个电话,同时找到#root 和#child2,或者
- 合并 find() 和 filter() 的结果
我看过其他关于“组合”结果的帖子,例如: How to combine two jQuery results 但这并不是真正的“组合”,而是“将更多结果附加到预先存在的结果”
我真正想做的是类似于$.extend():
var one = dom.find('.abc');
var two = dom.filter('.abc');
var three = combine(one, two);
// or one.somefcn(two); // One is augmented
// or three = one.somefcn(two); //
任何信息/想法将不胜感激
【问题讨论】:
-
我不明白为什么
.add()不能满足您的需求。 -
我想我遗漏了一些东西 - 你能简化你的预期输出和输入的内容