javascript getElementById appendChild 错误
我遇到了 javascript 问题,但我不知道如何解决它。 我有这段代码
function addtask(tab){
var tasktag = document.createElement('tr');
var task = document.getElementById(tab).getElementsByTagName('tr').length+1;
task = tab+'r'+task;
tasktag.setAttribute('id',task);
document.getElementById(tab).appendChild(tasktag);
var tasktag2 = document.createElement('td');
var task2 = document.getElementById(task).getElementsByTagName('td').length+1;
task2 = task+'c'+task2;
alert(task2);
tasktag2.setAttribute('id',task2);
tasktag2.innerHTML =''+task2+' <br><input id=\'btn1\' type=\'button\' value=\'remove\' onClick=\"removeElement(\''+num+'\',\'body\')\" />';
document.getElementById(task).appendChild(task2);
}`
,我得到“无法转换 JavaScript 参数 arg 0 [nsIDOMHTMLTableRowElement.appendChild]”(来自 firebug)。 虽然这段代码运行完美,
`var num;
function addlist(){
var divTag = document.createElement('table');
if(num==undefined){num = 1;}
else{numi = num.split("t")
num=parseInt(numi[1])+1;}
num = 't'+num;
divTag.setAttribute('id',num);
divTag.setAttribute('align','left');
divTag.setAttribute('style','margin:7px;');
divTag.setAttribute('border',1);
document.body.appendChild(divTag);
var divTag2 = document.createElement('tr');
var num2 = document.getElementById(num).getElementsByTagName('tr').length+1;
num2 = num+'r'+num2;
divTag2.setAttribute('id',num2);
document.getElementById(num).appendChild(divTag2);
var divTag3 = document.createElement('td');
var num3 = document.getElementById(num2).getElementsByTagName('td').length+1;
num3 = num2+'c'+num3;
divTag3.setAttribute('id',num3);
divTag3.innerHTML =''+num+' <br><input id=\'btn1\' type=\'button\' value=\'remove\' onClick=\"removeElement(\''+num+'\',\'body\')\" /><br><input id=\'btn2\' type=\'button\' value=\'task\' onClick=\"addtask(\''+num+'\')\" />';
document.getElementById(num2).appendChild(divTag3);
}
为什么会发生这种情况?谢谢大家的宝贵时间
i am having a problem with javascript and i dont know how to solve it.
i am having this code
function addtask(tab){
var tasktag = document.createElement('tr');
var task = document.getElementById(tab).getElementsByTagName('tr').length+1;
task = tab+'r'+task;
tasktag.setAttribute('id',task);
document.getElementById(tab).appendChild(tasktag);
var tasktag2 = document.createElement('td');
var task2 = document.getElementById(task).getElementsByTagName('td').length+1;
task2 = task+'c'+task2;
alert(task2);
tasktag2.setAttribute('id',task2);
tasktag2.innerHTML =''+task2+' <br><input id=\'btn1\' type=\'button\' value=\'remove\' onClick=\"removeElement(\''+num+'\',\'body\')\" />';
document.getElementById(task).appendChild(task2);
}`
i get "Couldn't convert JavaScript argument arg 0 [nsIDOMHTMLTableRowElement.appendChild]"(from firebug).
while this code works perfect
`var num;
function addlist(){
var divTag = document.createElement('table');
if(num==undefined){num = 1;}
else{numi = num.split("t")
num=parseInt(numi[1])+1;}
num = 't'+num;
divTag.setAttribute('id',num);
divTag.setAttribute('align','left');
divTag.setAttribute('style','margin:7px;');
divTag.setAttribute('border',1);
document.body.appendChild(divTag);
var divTag2 = document.createElement('tr');
var num2 = document.getElementById(num).getElementsByTagName('tr').length+1;
num2 = num+'r'+num2;
divTag2.setAttribute('id',num2);
document.getElementById(num).appendChild(divTag2);
var divTag3 = document.createElement('td');
var num3 = document.getElementById(num2).getElementsByTagName('td').length+1;
num3 = num2+'c'+num3;
divTag3.setAttribute('id',num3);
divTag3.innerHTML =''+num+' <br><input id=\'btn1\' type=\'button\' value=\'remove\' onClick=\"removeElement(\''+num+'\',\'body\')\" /><br><input id=\'btn2\' type=\'button\' value=\'task\' onClick=\"addtask(\''+num+'\')\" />';
document.getElementById(num2).appendChild(divTag3);
}
why is this happening?thank you all for your time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用表格时,您必须使用稍微不同的 JavaScript 函数:
尝试重新编写代码以使用
insertRow
而不是appendChild
,因为我很懒。When working with tables, you have to use slightly different JavaScript functions:
Try re-working your code to use
insertRow
instead ofappendChild
, as I am lazy.