innerHTML()是本机的,它返回DOM节点的内容(例如<span>I live inside a div.</span>
。externalHTML()则不是,它将包括当前的DOM节点(例如<div><span>I live inside a div.</span></div>
)中,这是可链接的jQuery版本。
$.fn.outerHTML = function(){
// IE, Chrome & Safari will comply with the non-standard outerHTML, all others (FF) will have a fall-back for cloning
return (!this.length) ? this : (this[0].outerHTML || (
function(el){
var div = document.createElement('div');
div.appendChild(el.cloneNode(true));
var contents = div.innerHTML;
div = null;
return contents;
})(this[0]));
}
翻译自: https://css-tricks.com/snippets/jquery/outerhtml-jquery-plugin/