helloworld!
document.write("hello world!");
function insertParagraph(text){ var str = "<p>"; str += text; str += "</p>"; document.write(str); }
insertParagraph("hello world!");
innerHTML方法
window.onload = function(){ var testdiv = document.getElementById("testdiv"); alert(testdiv.innerHTML); testdiv.innerHTML = "<p>hello word!</p>"; }
appendChild(),createTextNode()方法写
window.οnlοad=function(){ var para = document.createElement("p"); var testdiv = document.getElementById("testdiv"); testdiv.appendChild(para); var txt = document.createTextNode("Hello wordl!"); para.appendChild(txt); }
更复杂一些的<p>This is <em>my</em> content.</p>
var para = document.createElement("p"); var txt1 = document.createTextNode("This is "); var emphasis = document.createElement("em"); var txt2 = document.createTextNode("my"); var txt3 = document.createTextNode(" content."); para.appendChild(txt1); emphasis.appendChild(txt2); para.appendChild(emphasis); para.appendChild(txt3); var testdiv = document.getElementById("testdiv"); var testspan = document.getElementById("sp");
<div id="testdiv"><span id="sp">ss</span></div>