如何使用 DOM 更改 HTML 表格第一行中的按钮? (通过 DOM 添加和删除行)
在下面的小提琴中,我尝试使用 DOM 添加和删除表行。根据脚本,每行的最后一个字段将有一个删除按钮。但我希望第一个字段中的删除按钮是表外部的添加按钮。
只有第一行的按钮才应该是添加行按钮。该行的其余部分应该有删除按钮。我该怎么做?
jsfiddle - http://jsfiddle.net/7AeDQ/33/
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 );
}
HTML
<div id="POItablediv">
<input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()"/>
<table id="POITable" border="1">
<tr>
<td>POI</td>
<td>Latitude</td>
<td>Longitude</td>
<td>Delete?</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>
</tr>
</table>
Here in the below fiddle, i am trying to add and remove table rows using DOM. As per the script the last field of every row will have a delete button. But i want the delete button in the first field to be the add button which is outside the table.
Only the button in the first row should be add row button. rest of the row should have delete button as it is. How can i do this ?
jsfiddle - http://jsfiddle.net/7AeDQ/33/
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 );
}
HTML
<div id="POItablediv">
<input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()"/>
<table id="POITable" border="1">
<tr>
<td>POI</td>
<td>Latitude</td>
<td>Longitude</td>
<td>Delete?</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>
</tr>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过克隆现有行(和现有按钮)来创建新行。如果您希望第一行中的按钮行为与其他行不同,则需要在 insRow 函数中覆盖更改后的行为(就像当前对输入 id 所做的那样)。
尝试将代码更改为:
HTML:
JS:
You create new rows by cloning an existing row (and existing button). If you want the behaviour of the button in the first row to be different from other rows, you need to override that changed behaviour in your insRow function (as you do for input ids currently).
Try changing your code to:
HTML:
JS:
我可以猜测您的表将动态生成,这就是您遇到问题的原因。
一个 jQuery 解决方案,正如您标记的那样:
演示: http://jsfiddle.net /7AeDQ/59/
此外,您的删除按钮会出现多次,因此不要像您使用的那样使用固定的
id
:而是使用
class
或使用增量 id 类似id="delPOIbutton-1"
,id="delPOIbutton-2"
I can guess your table will be generated dynamically, that is why you having the issue.
A jQuery solution, as you have tagged it:
Demo: http://jsfiddle.net/7AeDQ/59/
Also your delete button will appear multiple times so don't use a fixed
id
for it as you used:Rather use a
class
or use incremental id likeid="delPOIbutton-1"
,id="delPOIbutton-2"