<span style="color: #008000;">//</span><span style="color: #008000;"> HTML:</span><span style="color: #008000;"> //</span><span style="color: #008000;"> <div id="container"><div id="d">This is a div.</div></div></span> <span style="color: #000000;"> container </span>= document.getElementById("container"<span style="color: #000000;">); d </span>= document.getElementById("d"<span style="color: #000000;">); console.log(container.firstChild.nodeName); </span><span style="color: #008000;">//</span><span style="color: #008000;"> logs "DIV"</span> <span style="color: #000000;"> d.outerHTML </span>= "<p>This paragraph replaced the original div.</p>"<span style="color: #000000;">; console.log(container.firstChild.nodeName); </span><span style="color: #008000;">//</span><span style="color: #008000;"> logs "P"</span> <span style="color: #008000;">//</span><span style="color: #008000;"> The #d div is no longer part of the document tree,</span><span style="color: #008000;"> //</span><span style="color: #008000;"> the new paragraph replaced it.</span>
While the element will be replaced in the document, the variable whose outerHTML
property was set will still hold a reference to the original element!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 4 | 45 (45) | 6 | 9.6 (probably earlier) | 3 |
t[t.innerText ? 'innerText' : 'textContent'] = v.n
it = currHeaderChildNodes[i].innerText || currHeaderChildNodes[i].textContent;