Using NodeLists

  Understanding a NodeList object and its relatives, NamedNodeMap and HTMLCollection, is critical to a good understanding of the DOM as a while. Each of those collections is considered "live", which is to say that they are updated when the document structure changes such that they are always current with the most accurate information. In reality, all NodeList objects are queries that are run against the DOM document whenever they are accessed. For instance, the following results in an infinite loop:

1 var divs = document.getElementsByTagName("div"),
2       i,
3       div;
4
5 for (i=0; i<div.length; i++){
6     div = document.createElement("div");
7     document.body.appendChild(div);
8 }

  The first part of this code gets an HTMLCollection of all <div> elements in the document. Since that collection is "live", any time a new <div> element is added to the page, it gets added into the collection. Since the browser doesn‘t want to keep a list of all the collections that were created, the collection is updated only when it is accessed again. This creates an interesting problem in terms of a loop such as the one in this example. Each tiem through the loop, the condition i<divs.length is being evaluated. That means the query to get all <div> elements is being run. Because the body of the loop creates a new <div> element and adds it to the document, the value of divs.length increments each time through the loop;

  Any time you want to iterate over a NodeList, it‘s best to initialize a second variable with the length and then compare the iterator to that variable, as shown in the following example:

1 var divs = document.getElementsByTagName("div"),
2       i,
3       len,
4       div;
5
6 for (i=0, len=divs.length; i<len; i++){
7     div = document.createElement("div");
8     document.body.appendChild(div);
9 }

  Generally speaking, it is best to limit the number of times you interact with a NodeList. Since a query is run against the document each time, try to cache frequently used values retrieved from a NodeList.

时间: 2024-10-13 10:13:59

Using NodeLists的相关文章

linux内核分析之内存管理

1.struct page 1 /* Each physical page in the system has a struct page associated with 2 * it to keep track of whatever it is we are using the page for at the 3 * moment. Note that we have no way to track which tasks are using 4 * a page, though if it

dojo/query源码解析

dojo/query模块是dojo为开发者提供的dom查询接口.该模块的输出对象是一个使用css选择符来查询dom元素并返回NodeList对象的函数.同时,dojo/query模块也是一个插件,开发者可以使用自定义的查询引擎,query模块会负责将引擎的查询结果包装成dojo自己的NodeList对象. require(["dojo/query!sizzle"], function(query){ query("div")... 要理解这个模块就要搞清楚两个问题:

jquery源码分析(三)——工具函数

jQuery.extend({ expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ),//生成字符串,使用Math.random生成随机数并使用正则去掉了非数字字符. //这里它作为HTMLElement或JS对象的属性名 isReady: true, error: function( msg ) { throw new Error( msg ); }, noop: function

DOM中的NodeList与HTMLCollection

最近在看<Javascript高级程序设计>的时候,看到了这样一句话:“理解NodeList和HTMLCollection,是从整体上透彻理解DOM的关键所在.”,所以觉得应该写一篇关于NodeList和HTMLCollection的博客来好好了解和总结下这方面的知识点. NodeList NodeList是一个节点的集合(既可以包含元素和其他节点),在DOM中,节点的类型总共有12种,通过判断节点的nodeType来判断节点的类型. 我们可以通过Node.childNodes和documen

underscore-1.8.3-analysis.js

1 // Underscore.js 1.8.3 2 // http://underscorejs.org 3 // (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors 4 // Underscore may be freely distributed under the MIT license. 5 // 中文注释 by hanzichi @https://github.com/h

ES6最具魔力的特性——生成器

ES6生成器(Generators)简介 我们从一个示例开始: function* quips(name) { yield "你好 " + name + "!"; yield "希望你能喜欢这篇介绍ES6的译文"; if (name.startsWith("X")) { yield "你的名字 " + name + " 首字母是X,这很酷!"; } yield "我们下次再见!

常用数组方法

js与jquery常用数组方法总结 昨天被问数组方法的时候,问到sort()方法是否会改变原来的数组.本来我猜是不会,也是这么说,马上我又觉得,知识这种东西,不确定的时候直接说不确定或不知道就好,只是凭借着不确定的猜测或者是记忆,害人害己,于是我又回答不知道.结果果然...我记错了0.0 还是自己总结测试一下比较靠谱,印象也比较深刻.欢迎大家提出相关意见或建议,提前谢谢哈~ 一.原生js方法 1.遍历数组 in(同时也是对象遍历属性的方法) var a = [1, 2, 3]; for (x i

Kernel那些事儿之内存管理(7) --- Slab(上)

前面讲的buddy system算法,分配内存的最小单位是一个页面(例如 4K).这对于大的内存申请比较适用.可是实际生活中,Kernel经常需要分配小的内存空间,比如几十个字节,这个时候怎么办呢? 不同的人可能会想到不同的解决办法. 对于财大气粗的富人一族,办法很简单:申请一个页面,只用其中的几十字节,剩下的直接丢掉. 对于锱铢必较的穷人一族,就不敢这么挥霍了:申请一个页面,然后记录好页面内哪些内存区用了,哪些还没用,没用的部分可以用来满足其他的内存分配请求.总之务必做到物尽其用. 很不幸,K

项目实践——Easyui中tree的使用

树状结构在项目中必不可少,这篇博客来学习一下Easyui的tree. 前台JSP <span style="font-size:14px;"><body> <div id="leftDiv" class="easyui-layout" fit="true"> <div region="center" border="false" border=&