javascript - 为什么 forEach 方法在 IE 中会崩溃?

标签 javascript

在 chrome/Firefox 上运行良好,仅在第二个 forEach 上中断,并且仅在 IE 上中断(我正在 IE11 上进行测试)。

Object doesn't support property or method 'forEach'

    moment(da).weekdaysInBetween(moment(da).add(14, 'day').format('MM/DD/YYYY')).forEach(function(nextDay){
   //console.log('first');
        thirteen.push(nextDay.format('MM/DD/YYYY'));
    });


   document.querySelectorAll('input[class="newname"]').forEach(function(input, index){
        input.value = thirteen[index];
      //  console.log('second');
    });

最佳答案

来自 ECMAScript 标准:

The forEach function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the forEach function can be applied successfully to a host object is implementation-dependent.

IE 表示它不支持查询返回的节点列表上的 .forEach 。您可以在列表上调用 Array.prototype.forEach,如以下简化示例所示:

Array.prototype.forEach.call( 
     document.querySelectorAll('input'),
     function(input, index){
        input.value = 'hello ' + index;
     }
);
<input type="text"><input type="text">

关于javascript - 为什么 forEach 方法在 IE 中会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46354000/

相关文章:

javascript - Polymer1.0如何实现长按事件?

javascript - 如何根据属性之一过滤 JSON 文件中的数据?

javascript - Google map - 发现较低路线的点顺序

javascript - 如何将 Soundcloud API SDK 3.0 on(finish, handler) 方法附加到我的 Play 函数?

javascript - 当中继器中的对象为空时 AngularJS 显示消息

javascript - 如何测量 Google 文档中的单词/插入符位置?

javascript - 仅在滚动 JS 上隐藏下拉菜单

javascript - jQuery 各函数不应用样式

javascript - 生成图形图表报告

javascript - 单击 button1 并将文本添加到 div,单击 button2 并删除文本和 css 样式 button1