Skip to content

Instantly share code, notes, and snippets.

@GitHub30
Last active January 11, 2018 07:05
Show Gist options
  • Save GitHub30/b4620ed580338d0b9e1ccd09ec6acdd2 to your computer and use it in GitHub Desktop.
Save GitHub30/b4620ed580338d0b9e1ccd09ec6acdd2 to your computer and use it in GitHub Desktop.
ParentNode.firstElementChild Polyfill for IE8, IE9 and Safari https://developer.mozilla.org/ja/docs/Web/API/ParentNode/firstElementChild
// Overwrites native 'firstElementChild' prototype.
// Adds Document & DocumentFragment support for IE9 & Safari.
// Returns array instead of HTMLCollection.
;(function(constructor) {
if (constructor &&
constructor.prototype &&
constructor.prototype.firstElementChild == null) {
Object.defineProperty(constructor.prototype, 'firstElementChild', {
get: function() {
var node, nodes = this.childNodes, i = 0;
while (node = nodes[i++]) {
if (node.nodeType === 1) {
return node;
}
}
return null;
}
});
}
})(window.Node || window.Element);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment