Ich habe das forEach-Polyfill am Anfang meiner JavaScript-Datei hinzugefügt, aber Internet Explorer sagt immer noch, dass es die Funktion nicht unterstützt.IE Unterstützt forEach nicht einmal mit Polyfill.
Ich möchte im Grunde eine Schleife über die Ergebnisse einer querySelector aber ich forEach auf einigen anderen Array-Objekte in meinem Skript verwenden.
Das alles funktioniert auf Chrome in Ordnung.
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback/*, thisArg*/) {
var T, k;
if (this === null) {
throw new TypeError('this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
if (arguments.length > 1) {
T = arguments[1];
}
k = 0;
while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
}
(function() {
var instance = null,
container;
// Constructor
this.MarvLightbox = function() {
// Initialise plugin
this.init();
};
// Initilise the plugin
MarvLightbox.prototype.init = function() {
document.querySelectorAll('[data-click]').forEach(function(e) {
e.addEventListener('click', [clickevent]);
});
};
}());
Sollte das Hinzufügen des Polyfill nicht dieses Problem mit IE beheben?
'querySelectorAll' gibt eine' NodeList', kein Array. –
[ 'Array.from'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from?v=control) – Emissary
@Emissary sehr schlechter Vorschlag - die ES6 und funktioniert nicht in IE – baao