为什么我在Internet Explorer中会出现这个错误:对象不支持“forEach”属性或方法?

12

我正在使用maven开发Jenkins插件的JavaScript部分,以下是我的代码:

   function arrayElements(element, index, array) 
     {
         var arrayPaths = element.split("\\");
         var projectSource = arrayPaths[2];
         var array = element.split("_");
         if (projectSource === global ) {             
             if (array[2]===filtro){
             document.getElementById("source").options.add(new Option(arrayPaths[3], element));
             }
         }
     }
    function fillCompiledSource(object, projects)
    {
        document.getElementById("source").innerHTML = "";        
        global = document.getElementById("branches").value;     
        projects.forEach(arrayElements)
    }
    var projects = new Array();</script><script>
    function fillCombo()
    {
         document.getElementById("source").innerHTML = "";
         global = document.getElementById("branches").value;     
         var array = document.getElementById("branches").value.split('/');
         global = array[1];
         projects.forEach(arrayElements)       
    }

这个问题只在Internet Explorer中出现,而且只有在文档模式为IE8标准时才会发生。我不知道原因是什么以及如何解决它。

Pd:Internet Explorer版本为10。


6
IE8不支持forEach方法。 - tymeJV
这个问题已经被问答过很多次了:https://dev59.com/oXRC5IYBdhLWcg3wFdFx - TigOldBitties
我的问题不同,因为我不能使用JQuery。我正在使用Maven开发Jenkins插件。 - davdomin
3个回答

11

是的,这是因为IE8 没有实现Array.forEach(以及许多其他更现代的JS方法)。如果你需要在IE8中工作,你必须模拟它 (请参阅兼容性部分)。

顺便说一句,MDN还有其他大多数不支持的方法的资源。


4
这可能有帮助。 要解决jQuery中的问题:
//This will fail in IE8
myObject.each(function(index, value){
 //your code goes here
});

//This will work in IE8 and all modern browsers
$.each(myObject, function(index, value){
 //your code goes here
});

@Daniel,谢谢。 - Joe L.

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接