IE7中的Element.prototype?

Bre*_*ent 7 javascript dom extend internet-explorer-7

我正在尝试扩展所有的dom元素,这样我就能得到并移除他们的孩子.功能如下(适用于FF和Chrome).在IE7中是否存在扩展基本dom对象的等价物?

if (!Element.get) {
Element.prototype.get = function(id) {
    for (var i = 0; i < this.childNodes.length; i++) {
        if (this.childNodes[i].id == id) {
            return this.childNodes[i];
        }
        if (this.childNodes[i].childNodes.length) {
            var ret = this.childNodes[i].get(id);
            if (ret != null) {
                return ret;
            }
        }
    }
    return null;
}
}

Element.prototype.removeChildren = function() {
    removeChildren(this);
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 6

这是一个简单的解决方法,在99%的情况下都足够了.它也可以根据您的脚本的要求完成:

if ( !window.Element ) 
{
    Element = function(){};

    var __createElement = document.createElement;
    document.createElement = function(tagName)
    {
        var element = __createElement(tagName);
        if (element == null) {return null;}
        for(var key in Element.prototype)
                element[key] = Element.prototype[key];
        return element;
    }

    var __getElementById = document.getElementById;
    document.getElementById = function(id)
    {
        var element = __getElementById(id);
        if (element == null) {return null;}
        for(var key in Element.prototype)
                element[key] = Element.prototype[key];
        return element;
    }
}
Run Code Online (Sandbox Code Playgroud)


Sho*_*og9 4

不会。 IE8 中会有一些有限的支持,但在那之前你最好找到另一个地方来挂起你的函数。