Internet Explorer 11 中的 SCRIPT438 错误 [英] SCRIPT438 Error in Internet Explorer 11

查看:35
本文介绍了Internet Explorer 11 中的 SCRIPT438 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究 JavaScript,一切都很好,直到我在 IE11 中打开我的页面.根据 Mozilla 网站 .forEach 从 IE9 开始支持.

I've been working on JavaScript lately, and everything was fine until I opened my page in IE11. as per Mozilla website .forEach is supported from IE9.

这是我得到的错误.

SCRIPT438:对象不支持forEach"属性或方法

SCRIPT438: Object doesn't support property or method 'forEach'

这是代码.

var link1 = document.querySelectorAll("nav a");
    var textbox = document.getElementById("OutputWindow");
    link1.forEach(function (element) {
        textbox.innerHTML += "<br/>" + element + "
";
        element.onclick = function () {
            alert("Hello!");
            console.log("hello!");
            confirm("Hello!");
        };
    });

我尝试过 polyfill,但让我觉得有趣的是,ArrayIE11 中有一个 forEach.

I tried polyfill, but to my amusement, Array has a forEach in IE11.

那我哪里出错了?

PS:这在 Chrome 中运行良好.

PS: This works fine in Chrome.

推荐答案

终于解开了谜团.

显然,IE9 及更高版本支持 Array.forEach 但不支持 NodeListquerySelector 返回.我尝试了 Array.from() 无济于事,因为它需要 ES6 或使用 ES6-shim.

Apparently, IE9 and above supports Array.forEach but not for NodeList, which querySelector returns. I tried Array.from() to no avail as it requires ES6 or use ES6-shim.

我所要做的就是从 nodeList 转换为 Array,我就是这样做的.

All I had to do was to convert from nodeList to Array, and I did by this.

Array.prototype.slice.call(document.querySelectorAll("nav a"), 0);

出现在问题 在 Javascript 中,将 NodeList 转换为数组的最佳方法是什么

这篇关于Internet Explorer 11 中的 SCRIPT438 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆