前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >javascript动态添加和删除表格

javascript动态添加和删除表格

作者头像
the5fire
发布2019-02-28 10:50:25
2.4K0
发布2019-02-28 10:50:25
举报

在web端开发时,动态添加和删除表格是很常见的,这里简单给出一点代码以供参考:

.. code:: html

<script type="text/javascript">
    var rowIndex = 0;
    function addOneLineOnClick() {
        var row=userList.insertRow(userList.rows.length);
        var col = row.insertCell(0);
        col.innerHTML = "<input align=center type=\"text\" name=\"username\" id=\"username\">";
        col = row.insertCell(1);
        col.innerHTML = "<input type=\"text\" name=\"usersex\" id=\"usersex\">";
        col = row.insertCell(2);
        col.innerHTML = "<input type=\"text\" name=\"userage\" id=\"userage\">";
        col = row.insertCell(3);
        col.innerHTML = "<input type='button' value='删除' id=btnDeleteLine name=btnDeleteLine onclick='return DeleteRow(\"row"+ rowIndex +"\")'>";
        row.setAttribute("id", "row" + rowIndex);
        rowIndex++;
    }

    function DeleteRow(rowTag){
        var i = userList.rows(rowTag).rowIndex;
        userList.deleteRow(i);
        rowIndex--;
    }
</script>

<table width="95%" border="1" cellpadding="0" cellspacing="0" name="userList" id="userList" align="center">
    <tr>
        <td nowrap>
            <div align="center">
            姓名
            </div>
        </td>
        <td nowrap>
            <div align="center">
            性别
            </div>
        </td>
        <td nowrap>
            <div align="center">
            年龄
            </div>
        </td>
        <td nowrap>
            <div align="center">
            删除
            </div>
        </td>
    </tr>
</table>
<p align="center">
<input name="btnAddLine" type="button" id="btnAddLine"
onClick="return addOneLineOnClick()" value="加入一行">
</p>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-01-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档