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. Spiral and Zigzag

    [LeetCode] 虽然感觉spiral matrix 两道题和 zigzag conversion 那道题没有太多联系,但是,毕竟都是相当于数学上的找规律题目. 这种优雅的题目就应该用下面这种优雅 ...

  2. BZOJ 1001 平面图转对偶图

    原图的面转成点,原图的边依旧边,只是连接的是两个面. 对偶图的点数=原图的面数 对偶图的边数=原图的边数(如果原边只属于一个面,则它为环边) #include<bits/stdc++.h> ...

  3. YII2.0.12兼容PHP7.2版本升级

    YII2.0.12兼容PHP7.2版本升级 报错信息: FastCGI sent in stderr: "PHP message: PHP Fatal error:  Cannot use ...

  4. CSS如何水平垂直居中?

    CSS如何水平垂直居中? 1.CSS如何实现水平居中? margin: 0 auto 2.CSS如何实现水平垂直居中? 首先设置一个div元素,设置背景颜色以便看出变化.代码如下: <!DOCT ...

  5. .Net界面开发神器—DevExpress官方汉化包免费下载!还在等什么?

    点击获取DevExpress v19.1.7新版试用下载 DevExpress Localization Service允许您创建一组自定义的附属程序集,要将语言包添加到程序集中,请查看本文中为大家列 ...

  6. MySQL 关于自定义函数的操作

    -- 函数 --> 模块化,封装,代码复用 create function 函数名([参数列表]) returns 数据类型 begin SQL语句: return 值: end; 示例: -- ...

  7. es6 模块编译 *** is not function

    今天学习vuejs,里面用到了es6的写法,遇到了一个很怪的问题,不知道有人遇到么. 安装的模块引用:import Vue from 'vue';(注意,Vue处没有{},如果加上这个就报错Uncau ...

  8. linux系统下nginx/mysql/php启动、停止、重启命令

    /usr/local/nginx/sbin/nginx /etc/init.d/mysql start /usr/local/php/sbin/php-fpm start   #nginx命令     ...

  9. CUDA-F-1-0-并行计算与计算机架构

    Abstract: 本文从总体上给出了CUDA编程的Big picture,后续所有的文章都在本文的基础上详细展开. Keywords: 并行计算,串行编程,并行编程,计算机架构,并行性,异构架构,C ...

  10. 2019 7.6 T2 虫洞

    虫洞(conch) [题目描述] HZY 现在在数轴原点处,她想跑到 2000001 这个点上.听说各路 神犇暑假里都在健♂身,所有 HZY 也想不要只是简单地跑步,于是她 决定在这条数轴上造虫洞,具 ...