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. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

  2. java HttpClientUtil帮助类

    自己写的java模拟请求帮助类,已经包含header头构造,会话session维持 package com.haozl.back.util; import java.io.File; import j ...

  3. Linux之more命令

    命令解释 more命令类似与cat命令,却比cat命令强大,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作. 命令说明 more [选项] 文件.. 命令选项 -d    显示帮助 ...

  4. python+Appium自动化:输入中文问题

    只要接触到app自动化,难免会遇到许多坑,今天说说解决中文输入的问题. 流程: 进入到淘宝应用,点击搜索栏,输入文字 一开始send_keys(“中文”)时,搜索栏一直没有出现文字,脚本也没有提示报错 ...

  5. maven项目pom.xml中parent标签的使用(转)

    原文地址:https://blog.csdn.net/qq_41254677/article/details/81011681 使用maven是为了更好的帮项目管理包依赖,maven的核心就是pom. ...

  6. MongoDB——增删改查

    文档结构: { "_id": ObjectId("5d5e5de597eb2f0b70005d1a"), , "word_records": ...

  7. css百分比值到底参考谁?

    一.元素宽高设置百分比 (1)width / min-width / max-width 参考块级父元素的宽度 (2)height / min-height / max-height 参考块级父元素的 ...

  8. No message错误

    Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message 错误原因是因为表单提交的 ...

  9. 通过PCI9030向外部RAM写数据失败现象

    我们的系统方案是:以9030作为PCI接口芯片,本地端映射了一片IDT70V06的双端口RAM.进行数据传输压力测试时,发现PC机向IDT70V06写数据偶尔会失败.这一问题是什么原因造成的呢? 最初 ...

  10. 十一、spring插件

    1.STS插件_ springsource-tool-suite插件各个历史版本 2.eclipse安装spring的插件 3.奇技淫巧:在spring官网上下载历史版本的spring插件,sprin ...