我有一些 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/