Home >Web Front-end >JS Tutorial >2 solutions to using outerHTML in Firefox_javascript tips
Everyone must be familiar with the innerHTML attribute of the DOM object, but the outerHTML is less useful. The innerHTML attribute returns the HTML contained in the DOM object from the start tag to the end tag, while the outerHTML attribute returns is the HTML including the tag of the DOM object itself. The following figure can well explain the difference between the two attributes:
outerHTML was originally a private attribute belonging to IE. You can view MSDN Description on: outerHTML Property(http://msdn.microsoft.com/en-us/library/ms534310(VS.85).aspx). Currently, IE, Chrome, Safari, and Opera all support this attribute. The problem is that outerHTML does not support Firefox. In Firefox, this attribute always returns undefined. Fortunately, HTML5 will add this attribute.
Let Firefox support the outerHTML attribute by extending the prototype of HTMLElement:
HTMLElement.prototype.__defineGetter__("canHaveChildren", function(){
return
!/^(area|base|basefont|
col|frame|hr|img|br|
input|isindex|link|meta
|param)$/.test(this.tagName.toLowerCase());
});
}
Compared with the above method, there is no need to touch the prototype, and the amount of code is much less. I believe there will be other solutions.