在 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/