使用 DOM 添加和删除 html 表格行时如何避免第一行被删除?
下面的小提琴来自另一个问题。它包含使用 JavaScript 动态添加和删除 html 表行的输出。小提琴中的代码给了我我想要的。但我的代码有一个问题。在逐行删除时,第一行也被删除。如何仅隐藏第一行的删除按钮?
HTML
<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>
JAVASCRIPT
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 );
}
以上是小提琴中的代码。如果您无法加载小提琴,希望这会有所帮助。有时会发生这种情况。
The below fiddle is from another question. It contains the output of a dynamic addition and deletion of html table rows using javascript. The code in the fiddle gave me what i want. But i have one problem with the code. While deleting the rows one by one, the first row is also getting deleted. How can i hide the delete button only in the first row ?
HTML
<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>
JAVASCRIPT
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 );
}
Above is the code in the fiddle. Hope this helps if you can't load fiddle. It happens sometimes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这看起来很优雅。
隐藏第一个:
然后显示新创建的tr:
Fiddle:
http://jsfiddle.net/Lh8KL/
This looks elegant.
Hide the first:
Then show the newly created tr:
Fiddle:
http://jsfiddle.net/Lh8KL/
只需添加以下语句:
这将获取对第三个单元格第一行中的
input
元素的引用,并在只有一行时隐藏它。更新的小提琴:http://jsfiddle.net/7AeDQ/23/
Just add this statement:
This gets a reference to the
input
element in the first row in the third cell and hides it if there is only one row.Updated fiddle: http://jsfiddle.net/7AeDQ/23/
只需在脚本中添加以下行
,在 HTML 中进行一些修改(在第一个删除按钮中将禁用添加为 true)
检查小提琴: http://jsfiddle.net/7AeDQ/25/
just add the following line in the script
Plus a little modification in the HTML (add disabled to true in the first delete button)
Check fiddle: http://jsfiddle.net/7AeDQ/25/