栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JavaScript动态添加/删除表行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JavaScript动态添加/删除表行

您可以仅克隆具有输入的第一行,然后获取嵌套的输入并更新其ID以添加行号(并与第一单元格相同)。

function deleteRow(row){    var i=row.parentNode.parentNode.rowIndex;    document.getElementById('POITable').deleteRow(i);}function insRow(){    var x=document.getElementById('POITable');       // deep clone the targeted row    var new_row = x.rows[1].cloneNode(true);       // get the total number of rows    var len = x.rows.length;       // set the innerHTML of the first row     new_row.cells[0].innerHTML = len;       // grab the input from the first cell and update its ID and value    var inp1 = new_row.cells[1].getElementsByTagName('input')[0];    inp1.id += len;    inp1.value = '';       // grab the input from the first cell and update its ID and value    var inp2 = new_row.cells[2].getElementsByTagName('input')[0];    inp2.id += len;    inp2.value = '';       // append the new row to the table    x.appendChild( new_row );}

下面的演示

function deleteRow(row) {  var i = row.parentNode.parentNode.rowIndex;  document.getElementById('POITable').deleteRow(i);}function insRow() {  console.log('hi');  var x = document.getElementById('POITable');  var new_row = x.rows[1].cloneNode(true);  var len = x.rows.length;  new_row.cells[0].innerHTML = len;  var inp1 = new_row.cells[1].getElementsByTagName('input')[0];  inp1.id += len;  inp1.value = '';  var inp2 = new_row.cells[2].getElementsByTagName('input')[0];  inp2.id += len;  inp2.value = '';  x.appendChild(new_row);}<div id="POItablediv">  <input type="button" id="addPOIbutton" value="Add POIs" /><br/><br/>  <table id="POITable" border="1">    <tr>      <td>POI</td>      <td>Latitude</td>      <td>Longitude</td>      <td>Delete?</td>      <td>Add Rows?</td>    </tr>    <tr>      <td>1</td>      <td><input size=25 type="text" id="latbox" /></td>      <td><input size=25 type="text" id="lngbox" readonly=true/></td>      <td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)" /></td>      <td><input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()" /></td>    </tr>  </table>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614839.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号