Javascript 将jquery集合添加到jquery集合时使用什么方法?

Javascript 将jquery集合添加到jquery集合时使用什么方法?,javascript,jquery,html,Javascript,Jquery,Html,所以我试了一下: var allItems = jQuery(); function additems(items) { allItems = allItems.pushStack(items); } additems(jQuery("#ul1").find("li").first()); additems(jQuery("#ul2").find("li").first()); additems(jQuery("#ul3").find("li").first()); allIte

所以我试了一下:

var allItems = jQuery();

function additems(items) {
    allItems = allItems.pushStack(items);
}

additems(jQuery("#ul1").find("li").first());
additems(jQuery("#ul2").find("li").first());
additems(jQuery("#ul3").find("li").first());


allItems.each(function() {
    jQuery("ul1").append(this);
});
她不在办公室工作

我需要的是在jQuery对象中保留一些项的集合。我希望它使用一个传递jQuery对象的函数。我还需要向集合中添加一个类似于上面所述的函数,以便可以在代码中的不同时间执行

我知道我可以通过以下方式解决问题:

function additems(items){
    items.each(function(){ allItems.pushStack(this);});
}
或者只是将它们作为HTML元素列表发送,但我更愿意这样做,我还没有找到一种干净有效的方法

谢谢

PS:我宁愿不需要插件

更新

让我再解释一下。我有一个函数做一些事情,它有一个元素,我可以从中提取元素

然后我想将这些元素发送到另一个函数,以便以后保存它们

function doSomething1()
{
    //Do something

    var ulElement = getFromSomewhere();
    additems(jQuery(ulElement).find("li"));

    // do something else
    return;
}

function additems(items)
{
    MyObject.allItems.pushStack(items)
}
与我的代码相比,JSFIDLE被简化了

新的jsfiddle具有固定的

答复

在将昆西的答案添加到我的小提琴上时,我得到的代码并未全部删除:

var allItems = jQuery();

function additems(items) {
    allItems = allItems.add(items);
}

additems(jQuery("#ul1").find("li").first());
additems(jQuery("#ul2").find("li").first());
additems(jQuery("#ul3").find("li").first());


allItems.each(function() {
    jQuery("ol").append(this);
});

这是可行的。

首先,如果你想按id选择,你必须在选择器中添加一个。 您可以使用add方法添加jquery集合


您不能仅仅通过使用jQuery选择器来获取项集合吗?
如果您使用类标记项目,您可以使用一个简单的选择器获得一个集合

我需要在函数中进行添加。詹姆斯,我想这回答了你的问题。想想看。@brianpeiris很抱歉,我错过了关于添加能够添加jQuery集合的内容。我需要的是allitems=allitems.additems我需要在函数中进行加法。它必须从另一个函数递增地添加到集合中,我不希望它直接访问集合。
var allItems = 


jQuery("#ul1").find("li").first()
.add(jQuery("#ul2").find("li").first())
.add(jQuery("#ul3").find("li").first());


allItems.each(function() {
    jQuery("ol").append($(this));
});