关于JS动态创建表格

关于JS动态创建表格,第1张

你这里有两个问题,一个赋值的时候不用改用innerText,二个是多余了两次嵌套循环

        function autocreate() {

            //创建table表格

            var table = document.createElement("table")

            table.setAttribute("border", "1")

            table.setAttribute("background", "red")

            //获取行数值

            var line = document.getElementById("line").value

            //获取列数值

            var list = document.getElementById("list").value

            for (var i = 0 i <= line i++) {

                //alert(line)

                //创建tr

                var tr = document.createElement("tr")

                for (var j = 0 j <= list j++) {

                    //alert(list)

                    //创建td

                    var td = document.createElement("td")

                    td.innerHtml = i + "*" + j

                    tr.appendChild(td)

                }

                table.appendChild(tr)

            }

            document.getElementById("d1").appendChild(table)

        }

<!DOCTYPE HTML>

<html>

<head>

<meta charset=UTF-8>

<title>table</title>

<style>

table {

    margin:auto

    width: 60%

    border: 1px solid black

    border-collapse: collapse

}

 

table caption {

    color: blue

    font: 34px consolas bold

}

 

table th, table td {

    border: 1px solid black

    text-align:center

}

 

table th {

    font: 20px consolas bold

    background-color: gray

}

 

table tr:nth-child(n+2){

    background-color: cyan

}

 

table tr:nth-child(2n+2)  {

    background-color: red

}

</style>

<script>

var data = [

    {id: "001", fullname: "张三", sex: "男", score: [98,33,48]},

    {id: "002", fullname: "李四", sex: "w", score: [11,22,33]},

    {id: "003", fullname: "kyo", sex: "m", score: [22,33,55]},

    {id: "004", fullname: "yamazaki", sex: "w", score: [99, 100, 80]}

]

 

var Student = function (id, fullname, sex, score)

{

    this.id = id

    this.fullname = fullname

    this.sex = sex

    this.score = score

}

 

var Score = function (chinese, math, english)

{

    this.chinese = chinese

    this.math = math

    this.english = english

}

 

var students = []

for (var i = 0 i < data.length i++)

{

    var d = data[i]

    var s = d["score"]

    students.push(new Student (d["id"], d["fullname"], d["sex"], new Score(s[0], s[1], s[2])))

}

 

onload = function ()

{

    var table = document.createElement("table")

    var tbody = document.createElement("tbody")

    table.appendChild(tbody)

    var caption = table.createCaption()

    caption.innerHTML = "学生成绩表"

    var tr = tbody.insertRow (0)

    var str = "学号,姓名,性别,语文,数学,英语".split(",")

    for (var i = 0 i < str.length i++)

    {

        var th = document.createElement("th")

        th.innerHTML = str[i]

        tr.appendChild (th)

    }

    for (var i = 0 i < students.length i++)

    {

        var tr = tbody.insertRow (tbody.rows.length)

        var obj = students[i]

        for (var p in obj)

        {

            var op = obj[p]

            if (p != "score")

            {

                var td = tr.insertCell (tr.cells.length)

                td.innerHTML = op

            }

            else

            {

                for (var p in op)

                {

                    var td = tr.insertCell (tr.cells.length)

                    td.innerHTML = op[p]

                }

            }

        }

    }

    document.body.appendChild(table)

}

</script>

<body>

 

</body>

</html>

<!DOCTYPE HTML>

<html>

<head>

    <title>Page Title</title>

    <meta http-equiv="Content-Type" content="text/html charset=UTF-8" />

    <style>

        * {

    margin: 0

    padding: 0

}

.wrap {

    margin: 20px auto

    width: 600px

}

table {

    width: 600px

    border: 1px solid black

    border-collapse: collapse

}

table input[type=button] {

    width: 50px

    height: 25px

    border: 0

    cursor: pointer

}

table input[type=button]:hover {

    background-color: #fd3

}

table input[type=text] {

    border-color: black

    border-width: 0 0 1px 0

    text-align: center

}

table textarea {

    width: 90%

    height: 100px

    resize: none

}

th,td {

    border: 1px solid black

    text-align: center

}

    </style>

    <script>

        var addRow = function(btn) {

            var tbody = table.tBodies[0]

            var row = btn.parentElement.parentElement.rowIndex

            if (btn.value == '-') {

                tbody.deleteRow(row)

            } else {

                var tr = tbody.insertRow(tbody.rows.length - 1)

                var arr = [

                 "<select><option>城际交通费</option></select>", 

                 "<input type='text' onkeyup='calc(this)' name='je' />", 

                 "<input type='text' />", 

                 "<input type='button' value='+' onclick='addRow(this)' />"

                ]

                for (var i = 0 i < 4 i++) {

                    var td = tr.insertCell(tr.cells.length)

                    td.innerHTML = arr[i]

                }

                btn.value = '-'

            }

        }

        var calc = function (txt){

         if(!/^[+-]?((\d+(\.\d*)?)|(\d*\.\d+))$/.test(txt.value)) {

         txt.value = txt.value.replace(/[^\+\-\.\d]/g, "")

         }

         var jes = document.getElementsByName("je")

         var sum = 0

         for(var i = 0 i < jes.length i++) {

         sum += parseFloat(jes[i].value)

         }

         zje.value = sum

        }

        onload = function (){

         dt.value = new Date().toLocaleString()

        }

    </script>

</head>

<body>

    <div class="wrap">

        <table id="table">

            <tbody>

                <tr>

                    <td>填写人:</td>

                    <td>

                        <input type="text" value="双q将" />

                    </td>

                    <td>填报时间:</td>

                    <td>

                        <input type="text" value="" id="dt" readonly />

                        </span>

                    </td>

                </tr>

                <tr>

                    <td>总金额:</td>

                    <td>¥

                        <input type="text" id="zje" readonly />

                    </td>

                    <td>状态:</td>

                    <td>

                        <input type="text" />

                    </td>

                </tr>

                <tr>

                    <th>项目</th>

                    <th>金额</th>

                    <th>费用说明</th>

                    <th> *** 作</th>

                </tr>

                <tr>

                    <td>

                        <select>

                            <option>城际交通费</option>

                        </select>

                    </td>

                    <td>

                        <input type="text" onkeyup="calc(this)" name="je" />

                    </td>

                    <td>

                        <input type="text" />

                    </td>

                    <td>

                        <input type="button" value="+" onclick="addRow(this)" />

                    </td>

                </tr>

                <tr>

                    <td>事由:</td>

                    <td colspan=3>

                        <textarea></textarea>

                    </td>

                </tr>

            </tbody>

        </table>

    </div>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11751969.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存