weixin_57649440 2022-12-14 14:21 采纳率: 66.7%
浏览 49
已结题

如何运用表单元素将成绩录入然后再点击删除

如何运用表单元素将成绩录入然后再点击删除,类似下面这个内容

img

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table{
            margin: 100px auto;
            width: 300px;
        }
        th{
            border: 1px solid black;
        }
        td{
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>分数</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
    <script>
        var tb = document.querySelector('tbody')
        function CreateItem(id,name,scroe){
            this.id = id
            this.name = name
            this.scroe = scroe
            this.render = function(){
                //  把数据插入到表格
                var row = document.createElement('tr')
                //tb.insertBefore()
                // 写入id
                var td = document.createElement('td')
                td.innerHTML = this.id
                row.appendChild(td)
                // 写入姓名
                td = document.createElement('td')
                td.innerHTML = this.name
                row.appendChild(td)
                // 写入分数
                td = document.createElement('td')
                td.innerHTML = this.scroe
                row.appendChild(td)
                // 操作
                td = document.createElement('td')
                var a = document.createElement('a')
                a.innerHTML = "删除"
                a.href = '#'
                a.onclick = function(){
                    tb.removeChild(row)
                }
                td.appendChild(a)
                row.appendChild(td)
                tb.appendChild(row)
            }
        }
        var item = new CreateItem(20201001,"张飞",100)
        item.render()
        item = new CreateItem(20201001,"赵云",100)
        item.render()
        item = new CreateItem(20201001,"刘备",100)
        item.render()
        item = new CreateItem(20201001,"乔丹",100)
        item.render()
    </script>
</body>
</html>


  • 写回答

3条回答 默认 最新

  • 心寒丶 全栈领域优质创作者 2022-12-14 14:42
    关注

    功能可以实现,如果格式有要求,改下css就行了

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            table{
                margin: 100px auto;
                width: 300px;
            }
            th{
                border: 1px solid black;
            }
            td{
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        学号:<input  id="id">
        姓名:<input id="name">
        分组:<input id="scroe">
        操作:<input type="button"  value="保存" onclick="save();">
        <table>
            <thead>
                <tr>
                    <th>学号</th>
                    <th>姓名</th>
                    <th>分数</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
        <script>
            var tb = document.querySelector('tbody')
            function CreateItem(id,name,scroe){
                this.id = id
                this.name = name
                this.scroe = scroe
                this.render = function(){
                    //  把数据插入到表格
                    var row = document.createElement('tr')
                    //tb.insertBefore()
                    // 写入id
                    var td = document.createElement('td')
                    td.innerHTML = this.id
                    row.appendChild(td)
                    // 写入姓名
                    td = document.createElement('td')
                    td.innerHTML = this.name
                    row.appendChild(td)
                    // 写入分数
                    td = document.createElement('td')
                    td.innerHTML = this.scroe
                    row.appendChild(td)
                    // 操作
                    td = document.createElement('td')
                    var a = document.createElement('a')
                    a.innerHTML = "删除"
                    a.href = '#'
                    a.onclick = function(){
                        tb.removeChild(row)
                    }
                    td.appendChild(a)
                    row.appendChild(td)
                    tb.appendChild(row)
                }
            }
            var item = new CreateItem(20201001,"张飞",100)
            item.render()
            item = new CreateItem(20201001,"赵云",100)
            item.render()
            item = new CreateItem(20201001,"刘备",100)
            item.render()
            item = new CreateItem(20201001,"乔丹",100)
            item.render()
             function save(){
             var tb = document.querySelector('tbody')
              var row = document.createElement('tr')
                    //tb.insertBefore()
                    // 写入id
                    var td = document.createElement('td')
                    td.innerHTML = document.getElementById("id").value;
                    row.appendChild(td)
                    // 写入姓名
                    td = document.createElement('td')
                    td.innerHTML = document.getElementById("name").value;
                    row.appendChild(td)
                    // 写入分数
                    td = document.createElement('td')
                    td.innerHTML = document.getElementById("scroe").value;
                    row.appendChild(td)
                    // 操作
                    td = document.createElement('td')
                    var a = document.createElement('a')
                    a.innerHTML = "删除"
                    a.href = '#'
                    a.onclick = function(){
                        tb.removeChild(row)
                    }
                    td.appendChild(a)
                    row.appendChild(td)
                    tb.appendChild(row)
            }
        </script>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月22日
  • 已采纳回答 12月14日
  • 创建了问题 12月14日

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败