JavaScript中的Object.prototype

Nie*_*sol 6 javascript internet-explorer prototype

我有一些JavaScript代码定义了一个函数getElementsByAttribute如下:

Object.prototype.getElementsByAttribute = function(attr) {
    var children = this.all || this.getElementsByTagName('*'),
        ret = [], i, c;
        for( i=0; i<children.length; i++) {
            c = children[i].getAttribute(attr);
            if( typeof c == "string" && c != "")
                ret.push(children[i]);
        }
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

这适用于我测试过的所有浏览器,除了Internet Explorer 7(并且可能更低) - 这些浏览器抛出"对象不支持此属性或方法".
我唯一能想到的是它不喜欢的是当我定义原型函数时已经创建了对象...
将函数定义为......好吧,一个"正常"函数并传递元素作为参数,有没有办法让这个工作在IE7及以下?

SLa*_*aks 6

IE DOM元素不是普通的Javascript对象,也不像您期望的那样继承原型.

http://perfectionkills.com/whats-wrong-with-extending-the-dom/