一次清空所有数据方法

<!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

 

转载于:https://www.cnblogs.com/langlang149/p/5626980.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值