为循环querySelectorAll [英] for of loop querySelectorAll

查看:171
本文介绍了为循环querySelectorAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mozilla声明:for循环将正确地循环NodeList对象。 (来源: https://developer.mozilla.org/en-US/ docs / Web / API / NodeList )然而,这不适用于Chrome 43。这是不正确的文档或浏览器的错误?



复制的示例在复选框的页面上使用的代码:

  var list = document.querySelectorAll('input [type = checkbox]'); 
for(var item of list){
item.checked = true;


解决方案

编辑:这是在Chrome 51中发货



杰克·阿奇博尔德发布了一个简单的修复方法

  NodeList.prototype [Symbol.iterator] = Array.prototype [Symbol.iterator] 

和for循环。

Mozilla states that "for of loops will loop over NodeList objects correctly". (source: https://developer.mozilla.org/en-US/docs/Web/API/NodeList) However, this doesn't work in Chrome 43. Is this incorrect documentation or a browser bug?

The copied example code used on a page with checkboxes:

var list = document.querySelectorAll( 'input[type=checkbox]' );
for (var item of list) {
  item.checked = true;
}

解决方案

Edit: This is shipping in Chrome 51.

Jake Archibald posted a simple fix:

NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]

And for of loops.

这篇关于为循环querySelectorAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆