关于javascript document.createDocumentFragment()


Posted in Javascript onApril 04, 2009

他支持以下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:

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.

Javascript 相关文章推荐
JavaScript 异步调用框架 (Part 3 - 代码实现)
Aug 04 Javascript
兼容IE和FF的js脚本代码小结(比较常用)
Dec 06 Javascript
JavaScript在IE和FF下的兼容性问题
May 19 Javascript
JavaScript对象反射用法实例
Apr 17 Javascript
jQuery实现仿新浪微博浮动的消息提示框(可智能定位)
Oct 10 Javascript
jQuery+ajax实现滚动到页面底部自动加载图文列表效果(类似图片懒加载)
Jun 07 Javascript
canvas压缩图片转换成base64格式输出文件流
Mar 09 Javascript
对vux点击事件的优化详解
Aug 28 Javascript
JS判断两个数组或对象是否相同的方法示例
Feb 28 Javascript
深入浅析Vue 中 ref 的使用
Apr 29 Javascript
Vue+element-ui添加自定义右键菜单的方法示例
Dec 08 Vue.js
vue-video-player 断点续播的实现
Feb 01 Vue.js
HTML 自动伸缩的表格Table js实现
Apr 01 #Javascript
Javascript 原型和继承(Prototypes and Inheritance)
Apr 01 #Javascript
说说掌握JavaScript语言的思想前提想学习js的朋友可以看看
Apr 01 #Javascript
setTimeout 不断吐食CPU的问题分析
Apr 01 #Javascript
js Flash插入函数免激活代码
Mar 31 #Javascript
响应鼠标变换表格背景或者颜色的代码
Mar 30 #Javascript
用JavaScript实现单继承和多继承的简单方法
Mar 29 #Javascript
You might like
如何用php获取程序执行的时间
2013/06/09 PHP
解析php中const与define的应用区别
2013/06/18 PHP
PHP中对各种加密算法、Hash算法的速度测试对比代码
2014/07/08 PHP
微信公众号OAuth2.0网页授权问题浅析
2017/01/21 PHP
PHP大文件分块上传功能实例详解
2019/07/22 PHP
YII2框架中ActiveDataProvider与GridView的配合使用操作示例
2020/03/18 PHP
关于B/S判断浏览器断开的问题讨论
2008/10/29 Javascript
JSON JQUERY模板实现说明
2010/07/03 Javascript
js 三级关联菜单效果实例
2013/08/13 Javascript
分享一款基于jQuery的视频播放插件
2014/10/09 Javascript
js实现的彩色方块飞舞奇幻效果
2016/01/27 Javascript
javascript每日必学之循环
2016/02/19 Javascript
JavaScript根据CSS的Media Queries来判断浏览设备的方法
2016/05/10 Javascript
JS基于正则截取替换特定字符之间字符串操作示例
2017/02/03 Javascript
基于Bootstrap table组件实现多层表头的实例代码
2017/09/07 Javascript
VueJS事件处理器v-on的使用方法
2017/09/27 Javascript
解决在vue项目中,发版之后,背景图片报错,路径不对的问题
2018/03/06 Javascript
基于bootstrap页面渲染的问题解决方法
2018/08/09 Javascript
bootstrap下拉分页样式 带跳转页码
2018/12/29 Javascript
茶余饭后聊聊Vue3.0响应式数据那些事儿
2019/10/30 Javascript
JS面向对象编程基础篇(二) 封装操作实例详解
2020/03/03 Javascript
vue.js中使用微信扫一扫解决invalid signature问题(完美解决)
2020/04/11 Javascript
Python ORM框架SQLAlchemy学习笔记之映射类使用实例和Session会话介绍
2014/06/10 Python
Python中使用wxPython开发的一个简易笔记本程序实例
2015/02/08 Python
python实现树形打印目录结构
2018/03/29 Python
python多线程之事件Event的使用详解
2018/04/27 Python
使用Python的Django和layim实现即时通讯的方法
2018/05/25 Python
PIL对上传到Django的图片进行处理并保存的实例
2019/08/07 Python
python爬虫构建代理ip池抓取数据库的示例代码
2020/09/22 Python
Python 虚拟环境工作原理解析
2020/12/24 Python
html5 Canvas画图教程(1)—画图的基本常识
2013/01/09 HTML / CSS
文言文形式的学生求职信
2013/12/03 职场文书
商务英语广告词大全
2014/03/18 职场文书
会议欢迎标语
2014/06/30 职场文书
党的群众路线教育实践活动教师自我剖析材料
2014/10/09 职场文书
交通事故案件代理词
2015/05/23 职场文书