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>
本文介绍了几种在Web页面中插入内容的方法,包括使用document.write、innerHTML、appendChild 和 createTextNode等API进行DOM元素的创建与插入。通过这些方法可以实现网页内容的动态更新。
2768

被折叠的 条评论
为什么被折叠?



