How to use outerHTML in JavaScript? [closed]

萝らか妹 提交于 2019-12-03 07:09:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!