首页 > Web开发 > 详细

[HTML 5] Using DocumentFragments

时间:2020-03-21 03:34:53      阅读:52      评论:0      收藏:0      [点我收藏+]

DocumentFragment can create virtual dom tree. It can helps to avoid layout reflow.. problems

 

Code has profermance problem:

const data = [Earth, Fire, Water, Air];

data.forEach(name => {
  const li = document.createElement(li);
  li.innerText = name;
  app.append(li);
});

 

Instead of each loop, we append one li tag, what we can do is accumlate all the li tags and append into DOM only once.

const data = [Earth, Fire, Water, Air];

const fragment = document.createDocumentFragment();

data.forEach(name => {
  const li = document.createElement(li);
  li.innerText = name;
  fragment.append(li);
});

app.append(fragment);

 

[HTML 5] Using DocumentFragments

原文:https://www.cnblogs.com/Answer1215/p/12535295.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!