简体   繁体   English

DocumentFragment正在丢失部分内容

[英]DocumentFragment is losing part of my content

So I'm basicaly just trying move element from one node to an other. 所以我基本上只是尝试将元素从一个节点移动到另一个节点。 I create a fragment and then append to it my children elements. 我创建了一个片段,然后将它添加到我的子元素中。

 const fragment = document.createDocumentFragment(); let sortedElements = [...document.querySelectorAll('.product')].sort((a,b) => b.dataset.blockSize - a.dataset.blockSize ); //Select elements sortedElements.forEach((e) => { console.log(e) //My 4 children displayed fragment.appendChild(e) }); console.log(fragment.children); //Only Two children in it fragment.childNodes.forEach((el) => { document.querySelector("#appendThere").appendChild(el); }) 
 <div class="product" data-object-size="4">Product 1</div> <div class="product" data-object-size="2">Product 2</div> <div class="product" data-object-size="1">Product 3</div> <div class="product" data-object-size="1">Product 4</div> <div id="appendThere"></div> 

Am I missunderstanding how does fragments works ? 我错过了解碎片是如何工作的吗?

It's strangely working on snippet... Even partialy working on my computer But its getting stranger. 它奇怪地在片段上工作......甚至在我的计算机上工作但是它变得更加陌生。

在此输入图像描述

在此输入图像描述

I think there is a change between the moment I print my variable and I explore it. 我认为在打印变量和探索变量之间存在变化。

You are mutating fragment.childNodes while iterating over it, which is causing the unexpected behavior. 您正在迭代fragment.childNodes同时迭代它,这会导致意外行为。 You just need to append fragment rather than appending each of it's children. 你只需要附加fragment而不是附加每个孩子的fragment

For example (fixed the element data attributes to correspond to the sort js in your example): 例如(修复元素数据属性以对应于示例中的排序js):

 const fragment = document.createDocumentFragment(); const sorted = [...document.querySelectorAll('.product')].sort((a,b) => { return b.dataset.blockSize - a.dataset.blockSize; }); sorted.forEach((elem) => { fragment.appendChild(elem); }); document.querySelector('#destination').appendChild(fragment); 
 <div class="product" data-block-size="3">Product 2</div> <div class="product" data-block-size="1">Product 3</div> <div class="product" data-block-size="4">Product 1</div> <div class="product" data-block-size="1">Product 4</div> <div id="destination"></div> 

Or, without using a document fragment (may not be a big performance difference if you are working with a limited number of elements). 或者,不使用文档片段(如果使用有限数量的元素,可能不会有很大的性能差异)。

 const destination = document.querySelector('#destination'); const sorted = [...document.querySelectorAll('.product')].sort((a,b) => { return b.dataset.blockSize - a.dataset.blockSize; }); sorted.forEach((elem) => { destination.appendChild(elem); }); 
 <div class="product" data-block-size="3">Product 2</div> <div class="product" data-block-size="1">Product 3</div> <div class="product" data-block-size="4">Product 1</div> <div class="product" data-block-size="1">Product 4</div> <div id="destination"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM