zl程序教程

您现在的位置是:首页 >  其它

当前栏目

关于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:
复制代码代码如下:

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.