我正在尝试扩展所有 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);
}
谢谢!
最佳答案
这是一个简单的解决方法,在 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;
}
}
关于javascript - IE7 中的 Element.prototype?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/597268/