jQuery向一个变量添加/链接元素

时间:2013-01-26 19:01:46

标签: jquery jquery-chaining jquery-append

我怎样才能把它变成更快的东西:

$.each(data, function(key, val) 
{ 
    rcItemsLi.eq(index++).append('<div class="rc-item-content clear hidden"><p class="rc-item-context">' + val[0] + '</p>' + val[1] + '</div>'); 
}); 
rcItemsContent = $('.rc-item-content');

在这个例子中,我首先将元素追加到我想要的位置,然后使用.rc-item-content选择器“查找”所有元素并将它们存储在rcItemsContent变量中。


例如:

$.each(data, function(key, val) 
{ 
    rcItemsContent.add($('<div class="rc-item-content clear hidden"><p class="rc-item-context">' + val[0] + '</p>' + val[1] + '</div>').appendTo(rcItemsLi.eq(index++))); 
});

在这个例子中,我想要实现的(当然我没有),是在变量中添加/链接元素并将其同时附加到我想要的位置。

1 个答案:

答案 0 :(得分:1)

.add创建一个新集合。

rcItemsContent = rcItemsContent.add(...