日期:2014-05-16  浏览次数:20311 次

js 添加行
我想按下“添加选项”按钮以后,在问题一行的下面添加选项,不过没有办法运行,报错Uncaught TypeError: Object #<HTMLInputElement> has no method 'insertCell' ,我的代码:


function $(id)
{
return document.getElementById(id);
}

function addRow(){
var tableObj = $("testTable");
var rowObj = tableObj.insertRow(-1);
rowObj.setAttribute("id","newRow");
var cellObj1 = rowObj.insertCell(-1);
var cellObj2 = rowObj.insertCell(-1);

cellObj1.innerHTML = '问&nbsp;&nbsp;&nbsp;&nbsp;题:<input type="text" id="question" value=""/>';
cellObj2.innerHTML ='<input type="button" value="添加选项" onclick="addOption()"/>'+ '<input type="button" value="删除问题" onclick="delRow(this)"/>';

}

function addOption(){
var questionObj=$("question");
var optionObj=questionObj.rowIndex;
questionObj.setAttribute("id","option");
var oObj1=questionObj.insertCell(-1);
var oObj2=questionObj.insertCell(-1);

oObj1.innerHTML ='选项<input type="text" id="oid"/><input type="text" value="" id="content" />';
oObj2.innerHTML ='<input type="button" value="添加" onclick="addOption()"/>'+ '<input type="button" value="删除" onclick="delRow(this)"/>';
}
javascript 添加行

------解决方案--------------------
getElementsByTagName("button") 应该为getElementsByTagName("input")
------解决方案--------------------
用createElement