• JS中的动态表格


    写法一:(有点啰嗦)

    //--------------XML DOM--------------------------------------
    function addTR(){
    //1.取三个框的值
    var username = document.getElementById("username").value;
    var email = document.getElementById("email").value;
    var age = document.getElementById("age").value;
    //2.添加tr,td
    var trObj = document.createElement("tr");
    var tdUsername = document.createElement("td");
    var tdEmail = document.createElement("td");
    var tdAge = document.createElement("td");
    var tdOp = document.createElement("td");
    //3.添加td的内容
    tdUsername.innerHTML=username;
    tdEmail.innerHTML=email;
    tdAge.innerHTML=age;
    tdOp.innerHTML="<input type='button' value='删除' onclick='deleteRowss(this)'/>";
    //4.搞关系
    trObj.appendChild(tdUsername);
    trObj.appendChild(tdEmail);
    trObj.appendChild(tdAge);
    trObj.appendChild(tdOp);

    //5.把tr添加到表格中
    document.getElementById("tabInfo").appendChild(trObj);

    }
    function deleteRowss(obj){
    var trObjss = obj.parentNode.parentNode;//tr
    trObjss.parentNode.removeChild(trObjss);
    }
    写法二:(推荐写法)
    //---------------------HTML DOM---------------------------------
    function addTR(){
    //1.取三个框的值
    var username = document.getElementById("username").value;
    var email = document.getElementById("email").value;
    var age = document.getElementById("age").value;
    //2.添加tr,td
    var tabinfoss = document.getElementById("tabInfo");
    //在表格中添加一行
    var trss = tabinfoss.insertRow();
    var tdUsernamess = trss.insertCell();
    var tdEmailss = trss.insertCell();
    var tdAgess = trss.insertCell();
    var tdOpss = trss.insertCell();
    //3.添加td的内容
    tdUsernamess.innerHTML=username;
    tdEmailss.innerHTML=email;
    tdAgess.innerHTML=age;
    tdOpss.innerHTML="<input type='button' value='删除' onclick='deleteRowss(this)'/>";


    }
    function deleteRowss(obj){
    //1.得到表格
    var tabinfoss = document.getElementById("tabInfo");

    //2.找到当前行的索引 rowIndex
    var rowindexss = obj.parentNode.parentNode.rowIndex;
    //3.删除
    tabinfoss.deleteRow(rowindexss);//当前行的索引
    }

    //--------------------------------------------------------------

    写法三 :IE中 貌似有问题
    function addTR(){
    //1.取三个框的值
    var username = document.getElementById("username").value;
    var email = document.getElementById("email").value;
    var age = document.getElementById("age").value;
    //2.添加tr,td
    var tabinfoss = document.getElementById("tabInfo");

    tabinfoss.innerHTML+="<tr><td>"+username+"</td><td>"+email+"</td><td>"+age+"</td><td><input type='button' value='删除' onclick='deleteRowss(this)'/></td></tr>";
    }

    function deleteRowss(obj){
    //1.得到表格
    var tabinfoss = document.getElementById("tabInfo");

    //2.找到当前行的索引 rowIndex
    var rowindexss = obj.parentNode.parentNode.rowIndex;
    //3.删除
    tabinfoss.deleteRow(rowindexss);//当前行的索引
    }

    //-------------------------------------------------------------------HTML 部分---------------------------------------------------------------//

    <body>
    <table align="center" cellpadding="10" cellspacing="3" >
    <tr>
    <td>姓名:<input type="text" id="username"/></td>
    <td>Email:<input type="text" id="email"/></td>
    <td>年龄:<input type="text" id="age"/></td>
    </tr>
    <tr>
    <td colspan="3" align="center"><input type="button" value="添加" onclick="addTR()"/></td>
    </tr>
    </table>

    <br/>
    <br/>
    <br/>
    <hr/>

    <table id="tabInfo" align="center" width="500" border="1">
    <tr>
    <td>姓名</td>
    <td>Email</td>
    <td>年龄</td>
    <td>操作</td>
    </tr>

    </table>

    </body>

  • 相关阅读:
    JSON 序列化类 南京酷得软件
    哈哈哈哈哈哈 找回记忆
    Presto
    (转)在Total Commander下使用SVN
    在ubuntu12.04,64位中安装lnmp一键包mysql的问题
    阿里云服务器上搭建php环境+redis
    在ubuntu12.04,64位中安装nginx+php+redis+mysql
    Redis篇:单线程I/O模型
    工具篇:apachehttpClient 和 jdk11HttpClient的使用
    技能篇:关于缓存数据的一致性探讨
  • 原文地址:https://www.cnblogs.com/weixiaozhekan/p/5164589.html
Copyright © 2020-2023  润新知