点评:随着科技的日新月异,各种浏览器层出不穷,最近的项目要求对同时支持IE和FireFox等各种浏览器,为此深受其苦,尤其是对于我这个习惯了使用诸如:"event.srcElement"、"outerHTML"、"parentElement"的人来说。许多在IE中使用起来得心应手的对象和方法,在W3C标准面
代码很简单,如下:
var pro = window.HTMLElement.prototype;
pro.__defineGetter__("outerHTML", function(){
var str = "<" + this.tagName;
var a = this.attributes;
for(var i = 0, len = a.length; i < len; i++){
if(a[i].specified){
str += " " + a[i].name + '="' + a[i].value + '"';
}
}
if(!this.canHaveChildren){
return str + " />";
}
return str + ">" + this.innerHTML + "</" + this.tagName + ">";
});
pro.__defineSetter__("outerHTML", function(s){
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var df = r.createContextualFragment(s);
this.parentNode.replaceChild(df, this);
return s;
});
pro.__defineGetter__("canHaveChildren", function(){
return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase());
});