zoukankan      html  css  js  c++  java
  • 动态表格(Java Script)

    动态表格(Java Script)

    基于纯核心DOM

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .inputForm{
                text-align: center;
            }
            .showForm{
                text-align: center;
                margin: 10px;
                border: 1px black solid;
            }
        </style>
    </head>
    <body>
        <div class="inputForm">
            <input type="text" id="name" name="name" placeholder="请输入姓名">
            <input type="text" id="gender" name="gender" placeholder="请输入性别">
            <input type="button" id="btn_add" value="添加" onclick="add();">
        </div>
    
        <div class="showForm">
            <table style="margin: auto">
                <thead>
                    <tr>
                        <td>序号</td>
                        <td>姓名</td>
                        <td>性别</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="4" id="sum">总人数: 0</td>
                    </tr>
                </tfoot>
                <tbody id="tbody">
                </tbody>
            </table>
        </div>
    
    
    <script>
        var cnt=1;
        setSum();
        function setSum(){
            var sum=document.getElementById("sum");
            sum.removeChild(sum.firstChild);
            sum.appendChild(document.createTextNode("总人数: "+(cnt-1)+""));
        }
        function add(qualifiedName, value){
            var tbody=document.getElementById("tbody");
            var tr=document.createElement("tr");
            var td_id=document.createElement("td");
            td_id.appendChild(document.createTextNode(cnt+""));
            cnt++;
            var td_name=document.createElement("td");
            td_name.appendChild(document.createTextNode(document.getElementById("name").value));
            var td_gender=document.createElement("td");
            td_gender.appendChild(document.createTextNode(document.getElementById("gender").value));
            tr.appendChild(td_id);
            tr.appendChild(td_name);
            tr.appendChild(td_gender);
            var td_a=document.createElement("td");
            var a=document.createElement("a");
            a.setAttribute("href", "javascript:void(0);");
            a.setAttribute("onclick", "remove(this);");
            a.appendChild(document.createTextNode("删除"));
            td_a.appendChild(a);
            tr.appendChild(td_a);
            tbody.appendChild(tr);
            setSum();
        }
        function remove(obj){
            var root=obj.parentNode.parentNode.parentNode;
            var p=obj.parentNode.parentNode;
            root.removeChild(p);
            cnt--;
            setSum();
        }
    </script>
    </body>
    </html>
    

    基于HTML DOM

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .inputForm{
                text-align: center;
            }
            .showForm{
                text-align: center;
                margin: 10px;
                border: 1px black solid;
            }
        </style>
    </head>
    <body>
        <div class="inputForm">
            <input type="text" id="name" name="name" placeholder="请输入姓名">
            <input type="text" id="gender" name="gender" placeholder="请输入性别">
            <input type="button" id="btn_add" value="添加" onclick="add();">
        </div>
    
        <div class="showForm">
            <table style="margin: auto">
                <thead>
                    <tr>
                        <td>序号</td>
                        <td>姓名</td>
                        <td>性别</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="4" id="sum">总人数: 0</td>
                    </tr>
                </tfoot>
                <tbody id="tbody">
                </tbody>
            </table>
        </div>
    
    <script>
        var cnt=1;
        setSum();
        function setSum(){
            var sum=document.getElementById("sum");
            sum.removeChild(sum.firstChild);
            sum.appendChild(document.createTextNode("总人数: "+(cnt-1)+""));
        }
        function add(qualifiedName, value){
            var tbody=document.getElementById("tbody");
            tbody.innerHTML+="<tr><td>"+cnt+"</td><td>"+
                document.getElementById("name").value+"</td><td>"+
                document.getElementById("gender").value+"</td><td>"+"<a href='javascript:void(0);' onclick='remove(this)'>删除</a>"+"</td></tr>";
            cnt++;
            setSum();
        }
        function remove(obj){
            var root=obj.parentNode.parentNode.parentNode;
            var p=obj.parentNode.parentNode;
            root.removeChild(p);
            cnt--;
            setSum();
        }
    </script>
    </body>
    </html>
    

    添加了一些选择特效

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .inputForm{
                text-align: center;
            }
            .showForm{
                text-align: center;
                margin: 10px;
                border: 1px black solid;
            }
            td{
                 100px;
            }
            .mouseover{
                background-color: pink;
            }
            .mouseout{
                background-color: white;
            }
        </style>
    </head>
    <body>
        <div class="inputForm">
            <input type="text" id="name" name="name" placeholder="请输入姓名">
            <input type="text" id="gender" name="gender" placeholder="请输入性别">
            <input type="button" id="btn_add" value="添加">
        </div>
    
        <div class="showForm">
            <table style="margin: auto">
                <thead>
                    <tr>
                        <td>序号</td>
                        <td>姓名</td>
                        <td>性别</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="4" id="sum">总人数: 0</td>
                    </tr>
                </tfoot>
                <tbody id="tbody">
                </tbody>
            </table>
        </div>
        <script>
            var cnt=1;
            setSum();
            function setSum(){
                var sum=document.getElementById("sum");
                sum.removeChild(sum.firstChild);
                sum.appendChild(document.createTextNode("总人数: "+(cnt-1)+""));
            }
            document.getElementById("btn_add").onclick=function (){
                var tbody=document.getElementById("tbody");
                tbody.innerHTML+="<tr><td>"+cnt+"</td><td>"+
                    document.getElementById("name").value+"</td><td>"+
                    document.getElementById("gender").value+"</td><td>"+"<a href='javascript:void(0);' onclick='remove(this)'>删除</a>"+"</td></tr>";
                cnt++;
                setSum();
                attention();
            }
            function remove(obj){
                var root=obj.parentNode.parentNode.parentNode;
                var p=obj.parentNode.parentNode;
                root.removeChild(p);
                cnt--;
                setSum();
            }
            function attention() {
                var trs = document.getElementsByTagName("tr");
                for (var i = 0; i < trs.length; i++) {
                    trs[i].onmouseover=function (){
                        this.className="mouseover";
                    }
                    trs[i].onmouseout=function (){
                        this.className="mouseout";
                    }
                }
            }
        </script>
    </body>
    </html>
    
  • 相关阅读:
    zindex在IE中的迷惑(一)
    IE “+” CSS Hack研究
    display:inlineblock的深入理解
    CSS解决未知高度垂直居中
    zindex在IE中的迷惑(二)
    三角的一个特殊做法
    图片垂直居中的使用技巧
    简单form标准化实例(一):整体布局
    简单form标准化实例(二):语义结构
    CSS定义通用透明效果
  • 原文地址:https://www.cnblogs.com/fromneptune/p/12525821.html
Copyright © 2011-2022 走看看