从零开始学_JavaScript_系列(30)——NodeList
2016-10-13 20:09
477 查看
(48)NodeList的forEach
github连接:
https://github.com/qq20004604/some_demo/tree/master/NodeList
①首先,NodeList不是数组,因此,在某些版本浏览器里,是没有forEach方法的(具体而言,我在chrome50版本里遇见过这个问题);
NodeList可以通过querySelectorAll来获得。但注意,通过document.getElementsByClassName获得的并不是NodeList
返回值是false
②NodeList是获取时决定的,而不是需要他时,实时生成,这点和数组类似,执行代码时确认
③也就是①中所说,在某些低版本浏览器里,比如我发现在chrome 50这个版本的浏览器中,有querySelectorAll这个方法,但是这个方法获取的NodeList不存在forEach方法,如果使用则会报错,但是chrome51可以正常运行。
解决办法是查看NodeList是否有forEach方法,如果没有的话,则将Array数组的forEach方法提供给他。
但我查看某些文章说不推荐这个方法,至于原因我并不是很明白
也可以使用for循环,将其放入一个数组中来替代这个方法,然后使用数组的forEach方法来替代这个方法
github连接:
https://github.com/qq20004604/some_demo/tree/master/NodeList
①首先,NodeList不是数组,因此,在某些版本浏览器里,是没有forEach方法的(具体而言,我在chrome50版本里遇见过这个问题);
NodeList可以通过querySelectorAll来获得。但注意,通过document.getElementsByClassName获得的并不是NodeList
console.log(document.getElementsByClassName("test") instanceof NodeList)
返回值是false
②NodeList是获取时决定的,而不是需要他时,实时生成,这点和数组类似,执行代码时确认
<!DOCTYPE HTML> <html> <head> </head> <body> <script> var nodeList = null; function getList() { nodeList = document.querySelectorAll(".test") console.log(document.getElementsByClassName("test") instanceof NodeList) console.log(nodeList instanceof NodeList) } function addTestNode() { var parent = document.getElementById("list"); var node = document.createElement("p"); node.innerHTML = "test"; node.classList.add("test"); parent.appendChild(node); } if (!("forEach" in NodeList)) { NodeList.prototype.forEach = Array.prototype.forEach; } function showNodeList() { document.getElementById("log").innerHTML = JSON.stringify(nodeList.length); console.log(nodeList); } </script> <p>以下是NodeList的结点列表</p> <div id="list"> </div> <p>以下显示NodeList的当前数组元素数,console可以查看当前NodeList有哪些元素</p> <div id="log"> </div> <button onclick="getList()">获取list</button> <button onclick="addTestNode()">添加结点</button> <button onclick="showNodeList()">显示list</button> </body> </html>
③也就是①中所说,在某些低版本浏览器里,比如我发现在chrome 50这个版本的浏览器中,有querySelectorAll这个方法,但是这个方法获取的NodeList不存在forEach方法,如果使用则会报错,但是chrome51可以正常运行。
解决办法是查看NodeList是否有forEach方法,如果没有的话,则将Array数组的forEach方法提供给他。
if (!("forEach" in NodeList)) { NodeList.prototype.forEach = Array.prototype.forEach; }
但我查看某些文章说不推荐这个方法,至于原因我并不是很明白
也可以使用for循环,将其放入一个数组中来替代这个方法,然后使用数组的forEach方法来替代这个方法
相关文章推荐
- 深入理解JavaScript系列(30):设计模式之外观模式
- 深入理解JavaScript系列(30):设计模式之外观模式
- 从零开始学_JavaScript_系列(十一)——dojo(4)(GRID表格进阶:格式化、style、数据获取、多重排序、点击事件)
- 从零开始学_JavaScript_系列(14)——dojo(7)(饼图,BorderContainer,hashchange,弹窗)
- 深入理解JavaScript系列(30):设计模式之外观模式
- HTML、CSS、JavaScript从零开始系列文章
- 从零开始学_JavaScript_系列(15)——js系列<4>(数值、字符串、对象、数组、函数、日期的基本方法)
- 从零开始学_JavaScript_系列(九)——dojo(2)(AJAX、时间控件、鼠标事件、样式修改、事件移除、消息发布订阅)
- 从零开始学_JavaScript_系列(八)——js系列<2>(事件触发顺序、文本读取、js编写ajax、输入验证、下拉菜单)
- 从零开始学_JavaScript_系列(16)——CSS<3>(文本、对齐、圆角、盒模型、背景)
- 深入理解JavaScript系列(30):设计模式之外观模式
- 从零开始学_JavaScript_系列(一)初步概念、工具选择、简单内容输出、加载js文件
- JavaScript系列:计算一个结果为30的加法智力题
- 从零开始学_JavaScript_系列(十)——dojo(3)(GRID表格创建、样式、列宽可变、排序、过滤器)
- 从零开始学_JavaScript_系列(五)——dojo(基础,动画移动,重力模拟,动画合并,添加标签)
- 深入理解JavaScript系列(30):设计模式之外观模式
- 深入理解JavaScript系列(30):设计模式之外观模式
- 从零开始学_JavaScript_系列(三)——CSS相关(基础、选择器、position、div)
- 从零开始学_JavaScript_系列(12)——jquery<2>(高度自适应,哈希地址及检测,单页面技术)
- 从零开始学_JavaScript_系列(17)——CSS<4>(定位、遮罩、float、弹性布局flex)