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的更多相关文章

  1. 关于Javascript作用域及作用域链的总结

    本文是根据以下文章以及<Javascript高级程序设计(第三版)>第四章相关内容总结的. 1.Javascript作用域原理,地址:http://www.laruence.com/200 ...

  2. 【2016-11-5】【坚持学习】【Day20】【Linq where in 语句】

    今天用到一个where  in  LINQ 语句 IEnumerable<Line> lines = wf.Lines.Where(n => n.RightNode == formR ...

  3. Using dojo/query(翻译)

    In this tutorial, we will learn about DOM querying and how the dojo/query module allows you to easil ...

  4. dojo/query源码解析

    dojo/query模块是dojo为开发者提供的dom查询接口.该模块的输出对象是一个使用css选择符来查询dom元素并返回NodeList对象的函数.同时,dojo/query模块也是一个插件,开发 ...

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

    ES6生成器(Generators)简介 我们从一个示例开始: function* quips(name) { yield "你好 " + name + "!" ...

  6. 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!【转载+整理】

    原文地址 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和for-of循环 生成器 Generators 模板字符串 不定参数和默认参数 解构 Destructu ...

  7. (笔记)Linux内核学习(九)之内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...

  8. dojo/dom dojo/domConstruct dojo/query

    dom.byId require(["dojo/dom", "dojo/domReady!"], function(dom) { var one = dom.b ...

  9. C#自动化IO/XML作业

    PS:这是我们公司自动化测试留的一个作业,虽然我不是自动化的,但是也做了一下. Friday, November 28, 2014 ​这个也是我根据自动化部门的那次作业自己分析写的,没有写打log的过 ...

随机推荐

  1. BZOJ 1006 完美消除序列&最大势算法&弦图

    K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.为了巩固三角关系,K国禁止四边关系,五边关系等等的存在.所谓N边关系 ...

  2. p1268树的重量 题解

    题面描述点此qwq. 正解开始. 一道茅塞顿开恍然大悟的题目: 第一眼看到这个题的时候,语文不好的我对着题目中的 这些,和: 这句话发呆半天,,,, 因为不关我怎么构建几何模型,我都不理解这句话.. ...

  3. vue插件——滚动监听 vue-scrollwatch

    造轮子的目的: 做项目的时候需要一个滚动监听的功能,html结构已经都写好了,不想使用vue组件的方式来写,因为不想改造html结构,于是花了几个小时做了一个简单的,使用vue指令方式来做的,项目上够 ...

  4. CAP原理与最终一致性 强一致性 弱一致性

    CAP原理中,有三个要素: 一致性(Consistency) 可用性(Availability) 分区容忍性(Partition tolerance) CAP原理指的是,这三个要素最多只能同时实现两点 ...

  5. HDU-3336-Count the string(扩展KMP)

    链接: https://vjudge.net/problem/HDU-3336 题意: It is well known that AekdyCoin is good at string proble ...

  6. 基于node.js的websocket上传小功能

    一.node.js 在目录里新建index.js var ws = require("nodejs-websocket"); console.log("开始建立连接... ...

  7. java输出乱码专题

    https://blog.csdn.net/liaoYu1887/article/details/82714727(其他) @Controller public class ItemCatContro ...

  8. js拖拽文件夹上传

    由于项目需要上传文件到服务器,于是便在文件上传的基础上增加了拖拽上传.拖拽上传当然属于文件上传的一部分,只不过在文件上传的基础上增加了拖拽的界面,主要在于前台的交互, 从拖拽的文件中获取文件列表然后调 ...

  9. k8s集群管理注意要点【持续更新】

    1.编写pod yaml文件时绑定调度标签,必须要给指定节点绑定标签,否则无法调度到指定节点上,报错: Events: Type Reason Age From Message ---- ------ ...

  10. PTA 重构二叉树

    重构二叉树 (25 分) 给出两个字符串,分别表示二叉树的先序遍历(根.左子树.右子树)和中序遍历(左子树.根.右子树)的结果. 例如,对于下面的二叉树,先序遍历结果是DBACEGF,中序遍历结果是A ...