首页 > 解决方案 > 如果我将 appendChild() 与尚未附加到 DOM 的元素一起使用,它会导致重排吗?

问题描述

例如,在这段代码中,该行是否myCustomDiv.appendChild(newElement);会导致回流?我认为它没有,因为该元素尚未附加到 DOM,但我在某处读到它确实会导致回流,那么为什么会发生这种情况?

const myCustomDiv = document.createElement('div');

for (let i = 1; i <= 200; i++) {
  const newElement = document.createElement('p');
  newElement.innerText = 'This is paragraph number ' + i;
  myCustomDiv.appendChild(newElement);
}

document.body.appendChild(myCustomDiv);

标签: javascriptweb

解决方案


推荐阅读