问题描述
我最近一直在使用JavaScript,直到在 IE11 中打开页面为止,一切都很好.根据 Mozilla网站 .forEach
IE9支持.
这是我得到的错误.
这是代码.
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
,但不支持NodeList
,querySelector
返回.我尝试Array.from()
无济于事,因为它需要 ES6 或使用 ES6-shim .
我要做的就是将nodeList
转换为Array
,而我正是这样做的.
Array.prototype.slice.call(document.querySelectorAll("nav a"), 0);
出现在问题中在Javascript中,将NodeList转换为数组的最佳方法是什么? I've been working on JavaScript lately, and everything was fine until I opened my page in IE11. as per Mozilla website This is the error I got. and this is the code. I tried polyfill, but to my amusement, Then where I'm going wrong? PS: This works fine in Chrome. Finally mystery solved. Apparently, IE9 and above supports All I had to do was to convert from as appeared in question In Javascript, what is the best way to convert a NodeList to an array 这篇关于Internet Explorer 11中的SCRIPT438错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!.forEach
is supported from IE9.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!");
};
});
Array
has a forEach
in IE11.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
to Array
, and I did by this.Array.prototype.slice.call(document.querySelectorAll("nav a"), 0);