【发布时间】:2013-01-10 12:24:27
【问题描述】:
我怎样才能把它变成更快的东西:
$.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++)));
});
在这个例子中,我想要实现的(当然我没有)是在变量中添加/链接元素并同时将其附加到我想要的位置。
【问题讨论】:
-
不要在循环内追加。
-
@BenM 为什么不能在循环中追加?
-
@ExplosionPills,你可以,但建议再次使用 > stage.learn.jquery.com/performance/append-outside-loop
-
@BenM 有没有办法将集合中的一个元素附加到其他集合中的一个元素,例如我得到了 3 个 div 的集合,以及 3 个段落的集合,我想将第 1 段附加到 div 1,第 2 段到 div 2 和第 3 段到 div 3,不使用循环?
标签: jquery jquery-chaining jquery-append