如何合并两个 jQuery 结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/323955/
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 08:57:31  来源:igfitidea点击:

How to combine two jQuery results

jquery

提问by nickf

How do you combine two jQuery search results? eg:

你如何组合两个 jQuery 搜索结果?例如:

var $allFoos = $('.foo'),
    $allBars = $('.bar')
    $allFoosAndBars = $allFoos + $allBars;

Obviously, I just made up that last line, but I hope it makes it sorta clear what I mean. To be clear, the example is greatly simplified, and it could be any arbitrary sets i'm talking about, so $('.foo, .bar')is notwhat I'm after.

很明显,我刚刚写了最后一行,但我希望它能让我明白我的意思。需要明确的是,该示例被大大简化,这可能是我说的任意集,所以$('.foo, .bar')不是我后。

回答by Simon

You can use add();

您可以使用add();

var $foos = $('.foo');

var $foosAndBars = $foos.add('.bar');

or

或者

var $allFoosAndBars = $allFoos.add($allBars);