当用户单击按钮时动态添加行。我制作了一个脚本,但它不起作用,请帮助我
<script>
var t;
t=2;
function Insert(){
var tab=document.getElementById("mytable");
var row=insertRow(t);
var cell1=insertCell(0);
var cell2=insertCell(1);
var cell3=insertCell(2);
var cell4=insertCell(3);
t=t+1;
cell2.setAttribute('contenteditable','true');
cell3.setAttribute('contenteditable','true');
cell4.setAttribute('contenteditable','true');
}
</script>
Html
<input type="button" onclick="Insert()" name="Insert Row" value="Insert Row" />
最佳答案
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell3 = row.insertCell(0);
var element2 = document.createElement("input");
element2.type = "text";
element2.name = "txtbox[]";
cell3.appendChild(element2);
}
<BODY>
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<TABLE id="dataTable" width="100px" border="1">
<TR>
<TD> <INPUT type="text" /> </TD>
</TR>
</TABLE>
</BODY>
关于javascript - 当用户单击按钮时动态添加行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39654878/