向页面写入一句话
helloworld!
document.write("hello world!");
function insertParagraph(text){
var str = "
";
str += text;
str += "
";document.write(str);
}
insertParagraph("hello world!");
innerHTML方法
window.onload = function(){
var testdiv = document.getElementById("testdiv");
alert(testdiv.innerHTML);
testdiv.innerHTML = "
hello word!
";}
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);
}
更复杂一些的
This is my content.
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");