关于javascriptdocument.createDocumentFragment()
关于 Javascriptdocument createDocumentFragment
2023-06-13 09:14:08 时间
他支持以下DOM2方法:
appendChild,cloneNode,hasAttributes,hasChildNodes,insertBefore,normalize,removeChild,replaceChild.
也支持以下DOM2?傩?
attributes,childNodes,firstChild,lastChild,localName,namespaceURI,nextSibling,nodeName,nodeType,nodeValue,ownerDocument,parentNode,prefix,previousSibling,textContent.
其他方法可以??ocumentFragment作?橐????担?ū热?ode的appendChild和insertBefore方法),??樱?ragment就可以被追加到父?ο笾小
Example:
复制代码代码如下:
appendChild,cloneNode,hasAttributes,hasChildNodes,insertBefore,normalize,removeChild,replaceChild.
也支持以下DOM2?傩?
attributes,childNodes,firstChild,lastChild,localName,namespaceURI,nextSibling,nodeName,nodeType,nodeValue,ownerDocument,parentNode,prefix,previousSibling,textContent.
其他方法可以??ocumentFragment作?橐????担?ū热?ode的appendChild和insertBefore方法),??樱?ragment就可以被追加到父?ο笾小
Example:
varfrag=document.createDocumentFragment();
frag.appendChild(document.createTextNode("IpsumLorem"));
document.body.appendChild(frag);
document.createDocumentFragment()说白了就是为了节约使用DOM。每次JavaScript对DOM的操作都会改变页面的变现,并重新刷新整个页面,从而消耗了大量的时间。为解决这个问题,可以创建一个文档碎片,把所有的新节点附加其上,然后把文档碎片的内容一次性添加到document中。
varoui=document.getElementById("oItem");
for(vari=0;i<10;i++)
{
varoli=document.createElement("li");
oui.appendChild(oli);
oli.appendChild(document.createTextNode("Item"+i));
}
上面的代码在循环中调用了oui.appendChild(oli),每次执行这条语句后,浏览器都会更新页面。其次下面的oui.appendChild()添加了文本节点,也要更新页面。所以一共要更新页面20次。
为了页面的优化,我们要尽量减少DOM的操作,将列表项目在添加文本节点之后再添加,并合理地使用creatDocumentFragment(),代码如下:
varoui=document.getElementById("oItem");
varoFragment=document.createDocumentFragment();
for(vari=0;i<10;i++){
varoli=document.createElement("li");
oli.appendChild(document.createTextNode("Item"+i));
oFragment.appendChild(oli);
}
oui.appendChild(oFragment);
W3C参考:http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-B63ED1A3
-------------------------------------------
DocumentFragmentisa"lightweight"or"minimal"Documentobject.Itisverycommontowanttobeabletoextractaportionofadocument"streeortocreateanewfragmentofadocument.Imagineimplementingausercommandlikecutorrearrangingadocumentbymovingfragmentsaround.ItisdesirabletohaveanobjectwhichcanholdsuchfragmentsanditisquitenaturaltouseaNodeforthispurpose.WhileitistruethataDocumentobjectcouldfulfillthisrole,aDocumentobjectcanpotentiallybeaheavyweightobject,dependingontheunderlyingimplementation.Whatisreallyneededforthisisaverylightweightobject.DocumentFragmentissuchanobject.
Furthermore,variousoperations--suchasinsertingnodesaschildrenofanotherNode--maytakeDocumentFragmentobjectsasarguments;thisresultsinallthechildnodesoftheDocumentFragmentbeingmovedtothechildlistofthisnode.
ThechildrenofaDocumentFragmentnodearezeroormorenodesrepresentingthetopsofanysub-treesdefiningthestructureofthedocument.DocumentFragmentnodesdonotneedtobewell-formedXMLdocuments(althoughtheydoneedtofollowtherulesimposeduponwell-formedXMLparsedentities,whichcanhavemultipletopnodes).Forexample,aDocumentFragmentmighthaveonlyonechildandthatchildnodecouldbeaTextnode.SuchastructuremodelrepresentsneitheranHTMLdocumentnorawell-formedXMLdocument.
WhenaDocumentFragmentisinsertedintoaDocument(orindeedanyotherNodethatmaytakechildren)thechildrenoftheDocumentFragmentandnottheDocumentFragmentitselfareinsertedintotheNode.ThismakestheDocumentFragmentveryusefulwhentheuserwishestocreatenodesthataresiblings;theDocumentFragmentactsastheparentofthesenodessothattheusercanusethestandardmethodsfromtheNodeinterface,suchasinsertBeforeandappendChild.
相关文章
- 内核杂谈——关于platform device 创建
- 关于cssText的差异
- 关于Harbor私有仓库的搭建及使用
- Maven:第一章:关于maven依赖死活都下载不了终极解决方案
- 关于基因的概念
- [哎]关于ftp扫描工具的激活成功教程问题
- 关于Ascii-Art的一些总结
- 关于 SAP Enterprise Portal 的前后端技术栈
- abap–关于sap地址,传真,邮箱的地址读取详解编程语言
- 你所不了解的 Bash:关于 Bash 数组的介绍
- Oracle解析关于段的概念(oracle关于段的说法)
- 有关于JSON的一些资料
- 参考:关于Javascript中实现暂停的几篇文章
- 关于firefox的ElementTraversal接口使用说明