HTML表格实现差异化新增行:点击Team列加整行、Members列仅加成员行
实现方案
你需要的交互可以通过HTML表格的rowspan属性搭配两个独立的新增函数实现,核心逻辑如下:
- 点击Team列加号:直接插入包含Team、Members两列的完整新行
- 点击Members列加号:先给当前行的Team单元格
rowspan值+1,再插入仅包含Members列的新行,无需新增Team列单元格
完整可运行代码
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" /> <table class="table table-bordered w-auto"> <thead> <tr> <th>Team</th> <th>Members</th> </tr> </thead> <tbody id="myList"> <script> // 新增包含两列的完整行 function addFullRow(){ var row = `<tr> <td class='form-group' style='position: relative'> <input type='text' class='form-control' placeholder='Enter the team'> <div class='justify-content-center' style='position: absolute; width: 100%; left: 50%;'> <a onclick="addFullRow()" class="fa fa-plus-circle" href="#"></a> </div> </td> <td class='form-group' style='position: relative'> <input type='text' class='form-control' placeholder='Enter the members'> <div class='justify-content-center' style='position: absolute; width: 100%; left: 50%;'> <a onclick="addMemberRow(this)" class="fa fa-plus-circle" href="#"></a> </div> </td> </tr>`; document.getElementById("myList").insertAdjacentHTML('beforeend', row); } // 仅新增Members列行 function addMemberRow(btn){ // 定位当前点击按钮所属的行 const currentTr = btn.closest('tr'); // 修改当前行Team列的跨行属性 const teamTd = currentTr.querySelector('td:first-child'); const currentRowspan = teamTd.getAttribute('rowspan') || 1; teamTd.setAttribute('rowspan', Number(currentRowspan) + 1); // 插入仅含Members列的新行 const memberRow = `<tr> <td class='form-group' style='position: relative'> <input type='text' class='form-control' placeholder='Enter the members'> <div class='justify-content-center' style='position: absolute; width: 100%; left: 50%;'> <a onclick="addMemberRow(this)" class="fa fa-plus-circle" href="#"></a> </div> </td> </tr>`; currentTr.insertAdjacentHTML('afterend', memberRow); } </script> <tr> <td class="form-group" style="position: relative"> <input type="text" class="form-control" placeholder="Enter the team"> <div class="justify-content-center" style="position: absolute; width: 100%; left: 50%;"> <a onclick="addFullRow()" class="fa fa-plus-circle" href="#"></a> </div> </td> <td class="form-group" style="position: relative"> <input type="text" class="form-control" placeholder="Enter the members"> <div class="justify-content-center" style="position: absolute; width: 100%; left: 50%;"> <a onclick="addMemberRow(this)" class="fa fa-plus-circle" href="#"></a> </div> </td> </tr> </tbody> </table>
主要修改说明
- 拆分了两个功能函数,分别对应两种新增场景
- 用
closest方法快速定位当前点击元素所属的行,不需要复杂的DOM遍历 - 动态修改Team列的
rowspan属性实现单元格自动跨行,完全符合HTML表格结构规范 - 修复了原代码中重复ID、非法input type的问题,新增的所有行也自带加号按钮,支持连续新增操作
内容的提问来源于stack exchange,提问作者Ismail86
相关产品推荐
相关产品推荐

