如何实现浏览器兼容版的element.children

来源:互联网 发布:淘宝花呗分期手续费 编辑:程序博客网 时间:2024/05/18 22:51

element.children这个获取节点子节点的方法支持ie9及以上版本,为了能够兼容低版本的ie,可参考一下的代码实现兼容,具体的参考资料是MDN上的实现方法:

// Overwrites native 'children' prototype.// Adds Document & DocumentFragment support for IE9 & Safari.// Returns array instead of HTMLCollection.(function(constructor) {    if (constructor &&        constructor.prototype &&        constructor.prototype.children == null) {        Object.defineProperty(constructor.prototype, 'children', {            get: function() {                var i = 0, node, nodes = this.childNodes, children = [];                while (node = nodes[i++]) {                    if (node.nodeType === 1) {                        children.push(node);                    }                }                return children;            }        });    }})(window.Node || window.Element);
原创粉丝点击