本頁只練什麼
能先在 DocumentFragment 組裝三個節點,再一次加入正式 DOM。
把建立與插入分開可讓 render 流程清楚,並避免每筆資料都直接操作 live container。
要修改的檔案:files/starter/main.js
定位:先在 append 前記錄 fragment.childElementCount。|依 starter-3 定位,只修改「DocumentFragment」的責任。
LESSON-16 · TOPIC 03
能先在 DocumentFragment 組裝三個節點,再一次加入正式 DOM。
能先在 DocumentFragment 組裝三個節點,再一次加入正式 DOM。
把建立與插入分開可讓 render 流程清楚,並避免每筆資料都直接操作 live container。
要修改的檔案:files/starter/main.js
定位:先在 append 前記錄 fragment.childElementCount。|依 starter-3 定位,只修改「DocumentFragment」的責任。
先結果,再原理;完成後要能說出自己看見的證據。
render 迴圈同時負責建立與插入,難以檢查中間結果。
起始狀態:空 list 與三筆文字資料。
fragmentBefore=3、domAfter=3、fragmentAfter=0、appendCalls=1。
驗收證據:fragmentBefore=3、domAfter=3、fragmentAfter=0
從陣列建立多個 DOM 節點並批次提交到容器。
常見混淆:fragment 不是複製節點;append 會移動其 children。
這段程式是理解起點,不是要你直接跳過 Starter。
先在看不見的暫存盒裝好三項,再一次放進畫面。
正式說法:DocumentFragment 是無 parent 的輕量 Document 節點;append 時其 children 會移入目標,fragment 隨後變空。
const fragment=document.createDocumentFragment(); fragment.append(li); list.append(fragment)。每一步都留下可觀察結果;如果沒看到,先看「卡住先查」。
檔案:files/starter/main.js 定位:先在 append 前記錄 fragment.childElementCount。
要做:先重新載入 Starter,記錄目前畫面、DOM、Computed、Console 或文件內容。
預期:能指出「先在 append 前記錄 fragment.childElementCount。」目前的缺口。
證據:保存 files/starter/main.js 的目前狀態,並指出下一步只會修改哪個檔案責任。
卡住先查:確認開啟的是 files/starter/main.js,且定位到 先在 append 前記錄 fragment.childElementCount。。
檔案:files/starter/main.js 定位:先在 append 前記錄 fragment.childElementCount。
要做:把三項加入 fragment,再一次 append 到 list。
預期:fragmentBefore=3、domAfter=3、fragmentAfter=0、appendCalls=1。
證據:fragmentBefore=3、domAfter=3、fragmentAfter=0
卡住先查:先在 append 前記錄 fragment.childElementCount。
檔案:files/starter/main.js 定位:先在 append 前記錄 fragment.childElementCount。
要做:重新整理頁面,再逐項比對預期結果與 evidence。
預期:fragmentBefore=3、domAfter=3、fragmentAfter=0、appendCalls=1。
證據:fragmentBefore=3、domAfter=3、fragmentAfter=0
卡住先查:先在 append 前記錄 fragment.childElementCount。
這個示範只讓你看懂概念,不會替你修改 Starter,也不會自動宣告完成。
勾選只是學習紀錄;真正完成仍要回到 Starter 的實際結果。
迴圈內只填 fragment。
迴圈結束才 list.append(fragment)。
完整解答與原因:三個 li 先 append 到 DocumentFragment,最後 list.append(fragment) 一次;children 會移到 list,所以 fragmentAfter=0。