关于javascript document.createDocumentFragment()

 更新时间:2009年04月04日 01:03:51   作者:  
documentFragment 是一個無父對象的document對象.
他支持以下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 作為一個參數,(比如Node的 appendChild和insertBefore 方法),這樣,fragment 就可以被追加到父對象中。
Example:
复制代码 代码如下:

var frag = document.createDocumentFragment();
frag.appendChild(document.createTextNode('Ipsum Lorem'));
document.body.appendChild(frag);

document.createDocumentFragment()说白了就是为了节约使用DOM。每次JavaScript对DOM的操作都会改变页面的变现,并重新刷新整个页面,从而消耗了大量的时间。为解决这个问题,可以创建一个文档碎片,把所有的新节点附加其上,然后把文档碎片的内容一次性添加到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));
}

上面的代码在循环中调用了oui.appendChild(oli),每次执行这条语句后,浏览器都会更新页面。其次下面的oui.appendChild()添加了文本节点,也要更新页面。所以一共要更新页面20次。
为了页面的优化,我们要尽量减少DOM的操作,将列表项目在添加文本节点之后再添加,并合理地使用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.

相关文章

  • 微信小程序setInterval定时函数新手使用的超详细教程

    微信小程序setInterval定时函数新手使用的超详细教程

    平时开发中为实现倒计时效果可以使用setInterval即可,下面这篇文章主要给大家介绍了关于微信小程序setInterval定时函数新手使用的超详细教程,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-08-08
  • JavaScript如何将数据处理成树形结构

    JavaScript如何将数据处理成树形结构

    这篇文章主要介绍了JavaScript如何将数据处理成树形结构问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 微信小程序webview实现长按点击识别二维码功能示例

    微信小程序webview实现长按点击识别二维码功能示例

    这篇文章主要介绍了微信小程序webview实现长按点击识别二维码功能,结合实例形式分析了webview二维码识别相关操作技巧,需要的朋友可以参考下
    2019-01-01
  • js图片上传的封装代码

    js图片上传的封装代码

    这篇文章主要为大家详细介绍了js图片上传的封装代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • javascript动态添加样式(行内式/嵌入式/外链式等规则)

    javascript动态添加样式(行内式/嵌入式/外链式等规则)

    添加CSS的方式有行内式、嵌入式、外链式、导入式,下面为大家详细介绍下javascript动态添加以上样式规则的方法,感兴趣的朋友可以参考下哈
    2013-06-06
  • 浅析$.getJSON异步请求和同步请求

    浅析$.getJSON异步请求和同步请求

    下面小编就为大家带来一篇浅析$.getJSON异步请求和同步请求。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • JavaScript实现更换背景图片

    JavaScript实现更换背景图片

    这篇文章主要为大家详细介绍了JavaScript实现更换背景图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • 解决Layui数据表格的宽高问题

    解决Layui数据表格的宽高问题

    今天小编就为大家分享一篇解决Layui数据表格的宽高问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • 原生JavaScript实现弹幕组件的示例代码

    原生JavaScript实现弹幕组件的示例代码

    这篇文章主要介绍了原生JavaScript实现弹幕组件的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 微信小程序+ECharts实现动态刷新的过程记录

    微信小程序+ECharts实现动态刷新的过程记录

    这篇文章主要给大家介绍了关于微信小程序+ECharts实现动态刷新的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05

最新评论