1

在 PrototypeJS 中,有没有更有效的方法将多个 Element 对象插入到一个元素中?这是我现在拥有的代码:

var calendar_elem = new Element('div', {
    'id': 'my_calendar'
}).insert({
    bottom: title_elem   // assume these are all Element objects
}).insert({
    bottom: days_of_week_elem
}).insert({
    bottom: month_calendar_elem
}).insert({
    bottom: footer_elem
});

索引可以bottom包含一个数组,还是必须只包含一个 HTML 元素/字符串?insert获取每个正在编辑的元素的 HTML、连接它们并将其传递给它会更有效bottom吗?谢谢!

4

2 回答 2

1

Dom 操作很昂贵,因为它们会在每次更改后导致页面重绘/重排。最好将您的 HTML 构建为字符串(或在内存中),然后一次性将它们插入 DOM。

于 2013-03-11T18:14:59.093 回答
0

这是我在发布问题几分钟后想到的一个解决方案:

[title_elem, days_of_week_elem, ...].each(function(obj)
{
    calendar_elem.insert({
        bottom: obj
    });
});

但我希望有一种内置于 PrototypeJS 中的方法来执行此操作。如果这是使代码更高效的唯一方法,请告诉我。谢谢。

于 2013-03-11T18:14:46.447 回答