首页 > 解决方案 > 转换 foreach 循环以与 Internet Explorer 兼容

问题描述

每个循环我都有以下内容:

function trackMeasurementChanged() {
                var weightInputs = document.querySelectorAll('[id*="HeightAndWeight_Weight"]');

                weightInputs.forEach(function(input) {
                    input.addEventListener('input', function(input) {
                        changedMeasurements.push("weight");
                    })
                });     
            }

Internet Explorer 不支持此功能,并且未通过某些单元测试,因此我尝试将语法转换为以下内容:

      for (var i = 0, len = weightInputs.Length; i < len; i++) {
                    var input = weightInputs[i];
                    console.log(input);
                    input.addEventListener('input', function (input) {
                        changedMeasurements.push("weight");
                    })
                }

^ 至少这是我的尝试,但现在代码不起作用。没有错误,只是不再将“重量”字符串推入数组。请谁能指出我哪里出错了?我不熟悉旧语法。

标签: javascriptloops

解决方案


推荐阅读