问题
How to use outerHTML in JavaScript? Thanks
回答1:
The outerHTML is the HTML of an element including the element itself. Contrast this with the innerHTML of the element, which is the HTML contained within an elements opening and closing tags. By definition, elements without both opening and closing tags do not have innerHTML.
Use the outerHTML when you want to completely replace an element and its contents. Use innerHTML when you only want to replace the contents of the element.
回答2:
function getHTML(node){
if(!node || !node.tagName) return '';
if(node.outerHTML) return node.outerHTML;
// polyfill:
var wrapper = document.createElement('div');
wrapper.appendChild(node.cloneNode(true));
return wrapper.innerHTML;
}
回答3:
outerHTML was originally a non-standard Internet Explorer property of an element, but now has cross-browser support. It returns the HTML of the element and its child elements. For elements that have parents, it can be set to replace the element and its descendants.
回答4:
Most of the most popular browsers support outerHTML. Firefox is an exception. For more information about outerHTML browser support visit http://www.quirksmode.org/dom/w3c_html.html.
If you can add jQuery to your page I recommend to include the plugin jQuery outerHTML.
来源:https://stackoverflow.com/questions/2483429/how-to-use-outerhtml-in-javascript