Last active
January 11, 2018 07:05
-
-
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
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
// 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