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:
var divs = document.getElementsByTagName("div"),
i,
div;
for (i=0; i<div.length; i++){
div = document.createElement("div");
document.body.appendChild(div);
}
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:
var divs = document.getElementsByTagName("div"),
i,
len,
div;
for (i=0, len=divs.length; i<len; i++){
div = document.createElement("div");
document.body.appendChild(div);
}
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.
Using NodeLists的更多相关文章
- 关于Javascript作用域及作用域链的总结
本文是根据以下文章以及<Javascript高级程序设计(第三版)>第四章相关内容总结的. 1.Javascript作用域原理,地址:http://www.laruence.com/200 ...
- 【2016-11-5】【坚持学习】【Day20】【Linq where in 语句】
今天用到一个where in LINQ 语句 IEnumerable<Line> lines = wf.Lines.Where(n => n.RightNode == formR ...
- Using dojo/query(翻译)
In this tutorial, we will learn about DOM querying and how the dojo/query module allows you to easil ...
- dojo/query源码解析
dojo/query模块是dojo为开发者提供的dom查询接口.该模块的输出对象是一个使用css选择符来查询dom元素并返回NodeList对象的函数.同时,dojo/query模块也是一个插件,开发 ...
- ES6最具魔力的特性——生成器
ES6生成器(Generators)简介 我们从一个示例开始: function* quips(name) { yield "你好 " + name + "!" ...
- 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!【转载+整理】
原文地址 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和for-of循环 生成器 Generators 模板字符串 不定参数和默认参数 解构 Destructu ...
- (笔记)Linux内核学习(九)之内核内存管理方式
一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...
- dojo/dom dojo/domConstruct dojo/query
dom.byId require(["dojo/dom", "dojo/domReady!"], function(dom) { var one = dom.b ...
- C#自动化IO/XML作业
PS:这是我们公司自动化测试留的一个作业,虽然我不是自动化的,但是也做了一下. Friday, November 28, 2014 这个也是我根据自动化部门的那次作业自己分析写的,没有写打log的过 ...
随机推荐
- Mac下 CMD常用命令
1.常用命令 pwd 当前工作目录 cd(不加参数) 进root cd(folder) 进入文件夹 cd .. 上级目录 cd ~ 返回root cd - 返 ...
- Zabbix Server设置主机监控
- centos6网络命令
ifconfig route netstat ss setup ip {link, addr, route}
- sysbench运行autogen.sh报错
./autogen.sh: 4: autoreconf: not found是在不同版本的 tslib 下执行 autogen.sh 产生.它们产生的原因一样,是因为没有安装automake 工具, ...
- WPF界面开发技巧大放送!DevExpress WPF在TreeListView中扩展N级
DevExpress广泛应用于ECM企业内容管理. 成本管控.进程监督.生产调度,在企业/政务信息化管理中占据一席重要之地.通过DevExpress WPF Controls,您能创建有着强大互动功能 ...
- 题解 【USACO 4.2.1】草地排水
[USACO 4.2.1]草地排水 Description 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫 ...
- 开源的任务系统 TaskManager
Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...
- jpa介绍
1.jpa的介绍 JPA是Java Persistence API的简称, 中文名为Java持久层API; 是JDK 5.0注解或XML描述对象-关系表的映射关系, 并将运行期的实体对象持久化到数据库 ...
- Python字符串运算符
下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python": 操作符 描述 实例 + 字符串连接 >>>a + b 'Hel ...
- Bootloader - Main system - Recovery的三角关系
原文地址:http://blog.csdn.net/myarrow/article/details/8115610 一.MTD分区:BOOT: boot.img,Linux kernel ...