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

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

问题描述

我最近一直在使用JavaScript,直到在 IE11 中打开页面为止,一切都很好.根据 Mozilla网站 .forEach IE9支持.

这是我得到的错误.

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

这是代码.

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

我尝试了polyfill,但令我高兴的是,Array IE11 中有一个forEach.

那我要去哪里错了?

PS:在Chrome中可以正常使用.

解决方案

最后的谜团解决了.

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

我要做的就是将nodeList转换为Array,而我正是这样做的.

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

出现在问题中Mozilla website .forEach is supported from IE9.

This is the error I got.

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

and this is the code.

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

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

Then where I'm going wrong?

PS: This works fine in Chrome.

解决方案

Finally mystery solved.

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.

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);

as appeared in question In Javascript, what is the best way to convert a NodeList to an array

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

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