解决Firefox下outerHTML不支持问题

网络整理 - 07-27

代码很简单,如下:


01 var pro = window.HTMLElement.prototype; 

02 pro.__defineGetter__("outerHTML", function(){ 

03     var str = "<" + this.tagName; 

04     var a = this.attributes; 

05     for(var i = 0, len = a.length; i < len; i++){ 

06         if(a[i].specified){ 

07             str += " " + a[i].name + '="' + a[i].value + '"'; 

08         } 

09     } 

10     if(!this.canHaveChildren){ 

11         return str + " />"; 

12     } 

13     return str + ">" + this.innerHTML + "</" + this.tagName + ">"; 

14 }); 

15 pro.__defineSetter__("outerHTML", function(s){ 

16     var r = this.ownerDocument.createRange(); 

17     r.setStartBefore(this); 

18     var df = r.createContextualFragment(s); 

19     this.parentNode.replaceChild(df, this); 

20     return s; 

21 }); 

22 pro.__defineGetter__("canHaveChildren", function(){ 

23     return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase()); 

24 });