兼容IE下无法forEach NodeList的bug

NodeList 对象是一个节点的集合,是由 Node.childNodesdocument.querySelectorAll返回的,实则是一个类数组对象

Although NodeList is not an Array, it is possible to iterate over it with forEach(). It can also be converted to a real Array using Array.from().
However, some older browsers have not implemented NodeList.forEach() nor Array.from(). This can be circumvented by using Array.prototype.forEach() — see this document's Example.

在主流浏览器和高版本IE下,可以直接使用forEach进行遍历,但对于IE11以下的版本,将无法执行遍历,下面提供一种方法对低版本IE做兼容

NodeList.prototype.forEach = Array.prototype.forEach;

直接在NodeList对象原型上指定forEach为Array的forEach,从而实现了NodeList的forEach遍历。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,136评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,143评论 0 13
  • 0. 写在前面 当你开始工作时,你不是在给你自己写代码,而是为后来人写代码。 —— Nichloas C. Zak...
    康斌阅读 10,723评论 1 42
  • 2018年2月6日,星期二,好天气 我们放假咯。 我们航世2018年春节假期: 2018年2月7日- 2018年2...
    贺琴说阅读 1,137评论 0 1
  • 又到高考季,一批寒窗苦读的学子面临人生第一考,考完或许金榜题名或许名落孙山,或许出国造梦或许就此打工糊口……一个十...
    草莓布丁猫阅读 2,187评论 0 0