JS动态添加表格行,删除表格行

这段代码展示了如何使用HTML、CSS和JavaScript实现一个简单的文件上传功能。用户可以选择文件,表格会显示文件名、类型、大小和上传日期,并提供删除按钮。文件信息存储在JavaScript数组中,便于操作。
摘要由CSDN通过智能技术生成

在这里插入图片描述

 <div>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th >文件名</th>
                    <th >文件类型</th>
                    <th >文件大小</th>
                    <th >上传日期</th>
                    <th></th>
                </tr>
            </thead>
            <tbody id="tmain"></tbody>

        </table>
    </div>
    <div>
        <input type="file" id="upfile" name="upfile" style="display:none"
               onchange="changeAgentContent()"/>
        <input type="button" class="btn btn-primary"
               onclick="document.getElementById('upfile').click()"
               value='选择...'/>
        <script type="text/javascript">
            var files=[];
            function changeAgentContent() {
                var value = document.getElementById("upfile").value;
                var split = value.split("\\");
                var fileType=value.split(".")
                var elementById = document.getElementById("tmain");
                var newRow=elementById.insertRow();
                var newTd0=newRow.insertCell();
                var newTd1=newRow.insertCell();
                var newTd2=newRow.insertCell();
                var newTd3=newRow.insertCell();
                var newTd4=newRow.insertCell();

                newTd0.innerText=split[split.length - 1];
                newTd1.innerText=fileType[fileType.length - 1];
                newTd2.innerText=document.getElementById("upfile").files[0].size/1000+"kb";
                newTd3.innerText=Hap.formatDate(new Date());
                newTd4.innerHTML='<span class="btn btn-danger" οnclick="del(this)">删除</span>';

                var fileMsg={
                    filename:newTd0.innerText,
                    fileType:newTd1.innerText,
                    filesize:newTd2.innerText,
                    fileDate:newTd3.innerText
                }
                files.push(fileMsg);
                console.log(files);

            }
            function  del(cc){
                var table = cc.parentNode.parentNode.parentNode;
                var td=cc.parentNode.parentNode;
                table.removeChild(td);

            }
        </script>
    </div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值