javascript - JavaScript 中的对象原型(prototype)

标签 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;
}

这适用于我测试过的所有浏览器,除了 Internet Explorer 7(可能更低)——这些浏览器会抛出“对象不支持此属性或方法”。
我唯一能想到它不喜欢的是当我定义原型(prototype)函数时已经创建了对象......
将函数定义为...好吧,一个“普通”函数并将元素作为参数传递,有什么方法可以使它在 IE7 及以下版本中工作?

最佳答案

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

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

关于javascript - JavaScript 中的对象原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4210927/

相关文章:

jquery - 主要的 IE8 定位问题

javascript - 在哪里添加字符串原型(prototype)

javascript - appendChild 或追加或插入 - 没有任何效果。 TD 插入错误的位置

javascript - Redux-form 调用外部表单组件提交

javascript - 对 getElementById 的说明

javascript - 有没有办法确定 blob URL 是否指向某些内容?

javascript - 控制台中正确的原型(prototype)链(具有对象继承)

javascript - 为什么我的 html 文件没有加载express.static?

javascript - 在删除之前取消绑定(bind)元素的事件是否有任何性能提升或必要性?

CSS/IE7 - div 中的文本未居中,div 后有额外的分隔符