我後來在
http://www.google.com.tw/search?q=cache ... t&hl=zh-TW
找到了.
代碼:
function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.setAttribute('type', 'text');
el.setAttribute('name', 'txtRow' + iteration);
el.setAttribute('size', '40');
cellRight.appendChild(el);
}
function removeRowFromTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
Anonymous 寫:
我在網路上偶然間找到了一篇post 有關javascript如何add row and delete row的 source code.
代碼:
function addRow()
{
var obj=document.getElementById("tbl");
var newNode = obj.children(0).cloneNode(true);
obj.appendChild(newNode);
}
function delRow()
{
var obj=document.getElementById("tbl");
if (obj.children.length>1) obj.removeChild(obj.children(0));
else alert("I won't delete the first one!");
}
看起來是for IE的. 請問若再mozilla底下 該怎麼修改呢? 或是哪已有ref可以參考.
謝謝.