weixin_57649440 2022-12-14 06: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 06: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>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    weixin_57649440 2022-12-14 07:07

    img

    1
    回复
    weixin_57649440 2022-12-14 07:13

    可不可以改成这样

    img

    回复
    心寒丶 回复 weixin_57649440 2022-12-14 07:17

    这样?前面加上字不就行了么

    img

    回复
    展开全部12条评论
查看更多回答(2条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 已采纳回答 12月14日
  • 创建了问题 12月14日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部