我有一些定义函数的 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(可能更低) - 这些浏览器会抛出“对象不支持此属性或方法”。
我唯一能想到的它不喜欢的是,当我定义原型函数时,对象已经被创建了......
如果没有将函数定义为...好吧,一个“正常”函数并将元素作为参数传递,有什么方法可以使其在 IE 7 及更低版本中工作?
IE DOM 元素不是普通的 Javascript 对象,并且不会像您期望的那样继承原型。
http://perfectionkills.com/whats-wrong-with-extending-the-dom/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)