电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> Javascript>>JavaScript之appendChild一例:

JavaScript之appendChild一例

来源:网络 | 2007-1-15 | (有16415人读过)

使用appendChild,不管是用getElementById().appendChild()还是用document.body.appendChild()都不能一次添加多个节点:

<html >
<head>
<title>A Simple Page</title>
</head>
<script language="javascript">
function newElement(){
var newElem = document.createElement("p");
newElem.setAttribute("id","newP");//newElem.id = "newP";
var newText = document.createTextNode("This is the second paragraph.");
newElem.appendChild(newText);
for(var i=0;i<5;i++)
document.getElementById("paragraph1").appendChild(newElem);
//document.getElementById("emphasis1").appendChild(newElem);
//document.body.appendChild(newElem);
}
</script>
<body>
 <p  id="paragraph1">
  This is the
  <em id="emphasis1">
  one and only
  </em>
  paragraph on the page.
 </p>
 <input name="" type="button" value="Click here!" onclick="newElement()" />
</body>
</html>
 
点击"Click here!"后输出结果是:
This is the one and only paragraph on the page.
This is the second paragraph.
 
Javascript热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号