JavaScript动态向表格添加行的实现方法

作者:像素漫游时间:2026-10-25 07:09:05浏览:474

本文介绍如何使用JavaScript在HTML表格中动态添加新行。通过演示代码展示添加行前后的效果对比,并提供完整的可运行示例。包含添加行按钮功能及代码复制、保存等交互操作,适用于需要动态生成表格内容的Web开发场景。

动态添加表格行效果对比

未添加行之前:


动态添加表格行效果对比

添加行之后:


动态添加表格行效果对比

演示代码:


完整实现代码示例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>给表格内部动态添加行</title> <style type="text/css"> .tableStyle { border-collapse: collapse; width:100%; } td { font-size:12px; height:25px; border:1px solid #CCD5E8; } .btn { font-size:12pt; color: #003399; border: 1px #003399 solid; color:#006699; border-bottom: #93bee2 1px solid; border-left: #93bee2 1px solid; border-right: #93bee2 1px solid; border-top: #93bee2 1px solid; background-color: #e8f4ff; cursor: pointer; font-style: normal ; width:20px; height:22px; font-family:Verdana;font-family:Georgia;_font-family:Tahoma; padding:0 10px 1px;padding:3px 3px 1px;_padding:0 4px 1px; line-height:18px;line-height:14px;_line-height:16px; } </style> </head> <body> <table class="tableStyle" id="OwnershipStructure"> <tr> <td width="15%" rowspan="2" id="StructureLeft1"> 标的企业  股权结构 </td> <td width="15%" rowspan="2" id="StructureLeft2">股东人数:人 </td> <td width="30%"> 前十名出资人的名称<strong>(做成动态,填几个显示几个)</strong> </td> <td width="30%">出资比例 </td> </tr> <tr id="StructureRight"> <td> <input id="Text1" type="text" /></td> <td> <input id="Text2" type="text" /><input id="btnAddRow" class="btn" onclick="AddStructureRow()" type="button" value="+" /> </td> </tr> <tr> <td>评估机构</td><td></td><td></td><td></td> </tr> <tr><td>评估机构</td><td></td><td></td><td></td> </tr> </table>  </body> </html> 

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

作者最新文章