解决Firefox下不支持outerHTML问题代码分享

Dawn ·
更新时间:2024-11-10
· 755 次阅读

代码很简单,如下:

代码如下:
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());
});



firefox

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章