我想按下“添加选项”按钮以后,在问题一行的下面添加选项,不过没有办法运行,报错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添加行

解决方案 »

  1.   

    innerHTML添加的不能直接执行脚本,你另外添加onclick事件cellObj2.innerHTML ='<input type="button" value="添加选项"/>' ......;var o =  cellObj2.getElementsByTagName("button");
    for (i=0;i<o.length;i++)
    {   
        if (o[i].value=="添加选项")
        o[i].onclick = function(){addOption();}  //或者  o[i].onclick =new Function("addOption()");
        if (o[i].value=="删除")
         o[i].onclick = function(){delRow(this);}
    }
      

  2.   

    getElementsByTagName("button") 应该为getElementsByTagName("input")