zoukankan      html  css  js  c++  java
  • 一次清空所有数据方法

    <!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
    努力但不功利~~!
  • 相关阅读:
    ssh
    datetime
    网络-sdn(2)
    django-drf
    Vscode前段常用插件
    Vscode离线安装插件
    CSS 实现绘制各种三角形
    flex布局
    js实现全选和取消全选
    购物车用Ajax向后台传参
  • 原文地址:https://www.cnblogs.com/langlang149/p/5626980.html
Copyright © 2011-2022 走看看