zoukankan      html  css  js  c++  java
  • 动态创建数据table

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            #box table {
                border-collapse: collapse;
                text-align: center;
            }
        </style>
    </head>
    
    <body>
        <div id="box"></div>
        <script>
            //先进行模拟一个数据
            var json = [{
                    naem: 'zw',
                    age: 19,
                    sex: "",
                    subject: '语文',
                    scre: 90
                },
                {
                    naem: 'zw',
                    age: 19,
                    sex: "",
                    subject: '语文',
                    scre: 90
                },
                {
                    naem: 'zw',
                    age: 19,
                    sex: "",
                    subject: '语文',
                    scre: 90
                },
                {
                    naem: 'zw',
                    age: 19,
                    sex: "",
                    subject: '语文',
                    scre: 90
                },
                {
                    naem: 'zw',
                    age: 19,
                    sex: "",
                    subject: '语文',
                    scre: 90
                },
                {
                    naem: 'zw',
                    age: 19,
                    sex: "",
                    subject: '语文',
                    scre: 90
                },
            ];
            var headData = ["姓名", "年龄", "性别", "科目", "分数", "操作"];
            var box = document.querySelector("#box");
            var table = document.createElement("table");
            box.appendChild(table);
            table.style.backgroundColor = "pink";
            table.style.width = "600px";
            table.border = "1px";
            var thead = document.createElement("thead");
            table.appendChild(thead);
            var tr = document.createElement("tr");
            thead.appendChild(tr);
            for (var i = 0; i < headData.length; i++) {
                var th = document.createElement("th");
                th.innerText = headData[i];
                tr.appendChild(th);
            }
            var tbody = document.createElement("tbody");
            table.appendChild(tbody);
            for (var j = 0; j < json.length; j++) {
                var tr = document.createElement("tr");
                tbody.appendChild(tr);
                for(var key in json[j]){
                    var td=document.createElement("td");
                    td.innerText=json[j][key];
                    tr.appendChild(td)   
                }
                var a=document.createElement('a');
                var td=document.createElement("td");
                a.href="javascript:;"
                a.innerText="删除";
                tr.appendChild(td);
                td.appendChild(a);
                a.onclick=function(e){
                   // tbody.removeChild(e.target.parentNode.parentNode);
                   setTimeout(function () {
                       e.target.parentNode.parentNode.parentNode.removeChild(e.target.parentNode.parentNode);
                     },2000)
                }
    
            }
        </script>
    </body>
    
    </html>
  • 相关阅读:
    POJ2888 Magic Bracelet [矩阵快速幂+Burnside+欧拉函数]
    数列的 GCD [计数问题]
    com组件的注册
    WCF 传输和接受大数据
    数据库中已存在名为 'View_Business' 的对象。
    windows 两个用户,默认其中一个用户登录
    用C#读取,写入ini文件
    小心得,关于串口
    未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad
    无法激活服务,因为它不支持 ASP.NET 兼容性
  • 原文地址:https://www.cnblogs.com/tuziling/p/10111241.html
Copyright © 2011-2022 走看看