一次清空所有数据方法

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>

<body>
<div id="content">
  <h1>1</h1>
  <h1>2</h1>
  <h1>3</h1>
  <h1>4</h1>
  <h1>5 </h1>
</div>

<script type="text/javascript">
function clearText()
{
    var content=document.getElementById("content");
    // 在此完成该函数
   
             for(var i=0;0<content.children.length;i++) //content.children.length是个随i变化的量,每次i循环,content.children.length都减1
              /*for (var i=content.childNodes.length-1;i>=0;i--) {
 var childNode = content.childNodes[i];
 content.removeChild(childNode); */  //使用此语句是因为content.children.length是个变量,可以放在开始,然后条件为i,此为倒序。数组不变的话使用正序和倒序都行,数组发生变化的时候使用倒序比较好。
          {
            //alert(content.children.length);
            //alert(content.children[0]);
            content.removeChild(content.children[0]);       //content.children[0]是移出后的content的第一个元素
            //alert(content.children[0]);
          };
};
</script> 
<button onclick="clearText()">清除节点内容</button>
</body>
</html>
View Code

 

posted @ 2016-06-29 15:18  浪浪的拐子  阅读(215)  评论(0编辑  收藏  举报