IE supports forEach(…) when invoked fromthe console but not when called from the code

前端 未结 3 1638
死守一世寂寞
死守一世寂寞 2021-01-12 06:57

I\'m running this snippet the console. In IE it produces the output just as expected. Running the same in Cr and FF for reference confirms the congruence of behavior.

<
3条回答
  •  鱼传尺愫
    2021-01-12 07:29

    Basically document.querySelectorAll would return a nodeList an array like object not an array. You have to convert it to an array before invoking array functions over that.

    var menus = document.querySelectorAll("ul.application>li>a");
    menus = [].slice.call(menus);
    menus.forEach(function(element) { ... });
    

    If your environment supports ES6 then you can use Array.from()

    var menus = document.querySelectorAll("ul.application>li>a");
    menus = Array.from(menus);
    menus.forEach(function(element) { ... });
    

提交回复
热议问题