關(guān)于javascript document.createDocumentFragment()
更新時(shí)間:2009年04月04日 01:03:51 作者:
documentFragment 是一個(gè)無(wú)父對(duì)象的document對(duì)象.
他支持以下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.
其他方法可以將documentFragment 作為一個(gè)參數(shù),(比如Node的 appendChild和insertBefore 方法),這樣,fragment 就可以被追加到父對(duì)象中。
Example:
var frag = document.createDocumentFragment();
frag.appendChild(document.createTextNode('Ipsum Lorem'));
document.body.appendChild(frag);
document.createDocumentFragment()說(shuō)白了就是為了節(jié)約使用DOM。每次JavaScript對(duì)DOM的操作都會(huì)改變頁(yè)面的變現(xiàn),并重新刷新整個(gè)頁(yè)面,從而消耗了大量的時(shí)間。為解決這個(gè)問(wèn)題,可以創(chuàng)建一個(gè)文檔碎片,把所有的新節(jié)點(diǎn)附加其上,然后把文檔碎片的內(nèi)容一次性添加到document中。
var oui=document.getElementById("oItem");
for(var i=0;i<10;i++)
{
var oli=document.createElement("li");
oui.appendChild(oli);
oli.appendChild(document.createTextNode("Item"+i));
}
上面的代碼在循環(huán)中調(diào)用了oui.appendChild(oli),每次執(zhí)行這條語(yǔ)句后,瀏覽器都會(huì)更新頁(yè)面。其次下面的oui.appendChild()添加了文本節(jié)點(diǎn),也要更新頁(yè)面。所以一共要更新頁(yè)面20次。
為了頁(yè)面的優(yōu)化,我們要盡量減少DOM的操作,將列表項(xiàng)目在添加文本節(jié)點(diǎn)之后再添加,并合理地使用creatDocumentFragment(),代碼如下:
var oui=document.getElementById("oItem");
var oFragment=document.createDocumentFragment();
for(var i=0;i<10;i++){
var oli=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
-------------------------------------------
DocumentFragment is a "lightweight" or "minimal" Document object. It is very common to want to be able to extract a portion of a document's tree or to create a new fragment of a document. Imagine implementing a user command like cut or rearranging a document by moving fragments around. It is desirable to have an object which can hold such fragments and it is quite natural to use a Node for this purpose. While it is true that a Document object could fulfill this role, a Document object can potentially be a heavyweight object, depending on the underlying implementation. What is really needed for this is a very lightweight object. DocumentFragment is such an object.
Furthermore, various operations -- such as inserting nodes as children of another Node -- may take DocumentFragment objects as arguments; this results in all the child nodes of the DocumentFragment being moved to the child list of this node.
The children of a DocumentFragment node are zero or more nodes representing the tops of any sub-trees defining the structure of the document. DocumentFragment nodes do not need to be well-formed XML documents (although they do need to follow the rules imposed upon well-formed XML parsed entities, which can have multiple top nodes). For example, a DocumentFragment might have only one child and that child node could be a Text node. Such a structure model represents neither an HTML document nor a well-formed XML document.
When a DocumentFragment is inserted into a Document (or indeed any other Node that may take children) the children of the DocumentFragment and not the DocumentFragment itself are inserted into the Node. This makes the DocumentFragment very useful when the user wishes to create nodes that are siblings; the DocumentFragment acts as the parent of these nodes so that the user can use the standard methods from the Node interface, such as insertBefore and appendChild.
appendChild, cloneNode, hasAttributes, hasChildNodes, insertBefore, normalize, removeChild, replaceChild.
也支持以下DOM2屬性:
attributes, childNodes, firstChild, lastChild, localName, namespaceURI, nextSibling, nodeName, nodeType, nodeValue, ownerDocument, parentNode, prefix, previousSibling, textContent.
其他方法可以將documentFragment 作為一個(gè)參數(shù),(比如Node的 appendChild和insertBefore 方法),這樣,fragment 就可以被追加到父對(duì)象中。
Example:
復(fù)制代碼 代碼如下:
var frag = document.createDocumentFragment();
frag.appendChild(document.createTextNode('Ipsum Lorem'));
document.body.appendChild(frag);
document.createDocumentFragment()說(shuō)白了就是為了節(jié)約使用DOM。每次JavaScript對(duì)DOM的操作都會(huì)改變頁(yè)面的變現(xiàn),并重新刷新整個(gè)頁(yè)面,從而消耗了大量的時(shí)間。為解決這個(gè)問(wèn)題,可以創(chuàng)建一個(gè)文檔碎片,把所有的新節(jié)點(diǎn)附加其上,然后把文檔碎片的內(nèi)容一次性添加到document中。
復(fù)制代碼 代碼如下:
var oui=document.getElementById("oItem");
for(var i=0;i<10;i++)
{
var oli=document.createElement("li");
oui.appendChild(oli);
oli.appendChild(document.createTextNode("Item"+i));
}
上面的代碼在循環(huán)中調(diào)用了oui.appendChild(oli),每次執(zhí)行這條語(yǔ)句后,瀏覽器都會(huì)更新頁(yè)面。其次下面的oui.appendChild()添加了文本節(jié)點(diǎn),也要更新頁(yè)面。所以一共要更新頁(yè)面20次。
為了頁(yè)面的優(yōu)化,我們要盡量減少DOM的操作,將列表項(xiàng)目在添加文本節(jié)點(diǎn)之后再添加,并合理地使用creatDocumentFragment(),代碼如下:
復(fù)制代碼 代碼如下:
var oui=document.getElementById("oItem");
var oFragment=document.createDocumentFragment();
for(var i=0;i<10;i++){
var oli=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
-------------------------------------------
DocumentFragment is a "lightweight" or "minimal" Document object. It is very common to want to be able to extract a portion of a document's tree or to create a new fragment of a document. Imagine implementing a user command like cut or rearranging a document by moving fragments around. It is desirable to have an object which can hold such fragments and it is quite natural to use a Node for this purpose. While it is true that a Document object could fulfill this role, a Document object can potentially be a heavyweight object, depending on the underlying implementation. What is really needed for this is a very lightweight object. DocumentFragment is such an object.
Furthermore, various operations -- such as inserting nodes as children of another Node -- may take DocumentFragment objects as arguments; this results in all the child nodes of the DocumentFragment being moved to the child list of this node.
The children of a DocumentFragment node are zero or more nodes representing the tops of any sub-trees defining the structure of the document. DocumentFragment nodes do not need to be well-formed XML documents (although they do need to follow the rules imposed upon well-formed XML parsed entities, which can have multiple top nodes). For example, a DocumentFragment might have only one child and that child node could be a Text node. Such a structure model represents neither an HTML document nor a well-formed XML document.
When a DocumentFragment is inserted into a Document (or indeed any other Node that may take children) the children of the DocumentFragment and not the DocumentFragment itself are inserted into the Node. This makes the DocumentFragment very useful when the user wishes to create nodes that are siblings; the DocumentFragment acts as the parent of these nodes so that the user can use the standard methods from the Node interface, such as insertBefore and appendChild.
相關(guān)文章
uniapp開(kāi)發(fā)APP之強(qiáng)制更新和熱更新的實(shí)現(xiàn)
使用uni-app開(kāi)發(fā),可將代碼編譯到iOS、Android、微信小程序等多個(gè)平臺(tái),升級(jí)時(shí)也需考慮多平臺(tái)同步升級(jí),下面這篇文章主要給大家介紹了關(guān)于uniapp開(kāi)發(fā)APP之強(qiáng)制更新和熱更新的相關(guān)資料,需要的朋友可以參考下2022-12-12JavaScript動(dòng)態(tài)添加css樣式和script標(biāo)簽
這篇文章主要介紹了JavaScript動(dòng)態(tài)添加css樣式和script標(biāo)簽的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07JavaScript實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08文件上傳,iframe跨域數(shù)據(jù)提交的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇文件上傳,iframe跨域數(shù)據(jù)提交的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11JS highcharts動(dòng)態(tài)柱狀圖原理及實(shí)現(xiàn)
這篇文章主要介紹了JS highcharts動(dòng)態(tài)柱狀圖原理及實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10