设计学院-ui163 致力于网页标准 网页设计 web标准与ui设计。

JavaScript之appendChild一例

上一篇上一篇 | 下一篇下一篇发表时间:2007-10-19 16:34:41
使用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.
[本日志由 ui163 于 2007-10-19 16:34:41 编辑]
上一篇上一篇:关于DOM事件模型的两件事
下一篇下一篇:window.open详解
文章来自:设计学院
收藏到网摘: 收藏到QQ书签
Tags:

相关内容

网站建设导航→

相关内容

版权隐私 ┊ 免责声明  ┊ 友情链接 ┊ 网站地图  ┊ 联系我们

Copyrights©2006 UI163.com All Rights Reserved 2007

 豫ICP备07501346号