Created
May 31, 2019 16:15
-
-
Save AlekVolsk/8bc596e7b38cdfcb7a28ba778d838c63 to your computer and use it in GitHub Desktop.
IE11 polyfill's: matches, closest, forEach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
// matches | |
if (!Element.prototype.matches) { | |
Element.prototype.matches = Element.prototype.matchesSelector || | |
Element.prototype.webkitMatchesSelector || | |
Element.prototype.mozMatchesSelector || | |
Element.prototype.msMatchesSelector; | |
} | |
// closest | |
if (!Element.prototype.closest) { | |
Element.prototype.closest = function (css) { | |
var node = this; | |
while (node) { | |
if (node.matches(css)) return node; | |
else node = node.parentElement; | |
} | |
return null; | |
}; | |
} | |
// forEach | |
if (!Array.prototype.forEach) { | |
Array.prototype.forEach = function (callback, thisArg) { | |
thisArg = thisArg || window; | |
for (var i = 0; i < this.length; i++) { | |
callback.call(thisArg, this[i], i, this); | |
} | |
}; | |
} | |
if (typeof NodeList.prototype.forEach !== "function") { | |
NodeList.prototype.forEach = Array.prototype.forEach; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment