当前位置: 代码迷 >> Web前端 >> 向页面写下一句话
  详细解决方案

向页面写下一句话

热度:213   发布时间:2012-10-24 14:15:58.0
向页面写入一句话

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.onload=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>
  相关解决方案