本文介绍了“无法将方法'appendchild'调用为null" ...错误...为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码...
var main = document.getElementById("section"); //gets node for main window
var para1 = document.createElement("p"); //creates paragraph node
var para2 = document.createElement("p"); // creates another paragraph node
var req = request.responseXML; //gets xml data
var nodeList = req.getElementsByTagName("line"); //gets all nodes with line as tag
var nodeArray = new Array(); //holds only lines that are part of code
para1.appendChild(document.createTextNode(req.getElementsByTagName("description")[0].childNodes[0].nodeValue)); //creates text node to put inside paragraph node
para2.appendChild(document.createElement("table")); //creates a table tag and puts inside para2
for(var i=0; i<nodeList.length; i++)
{
if(nodeList[i].getAttribute("st") == "no")
{
document.getElementById("table").appendChild(document.createElement("tr")); //creates a table row for each line participating in quiz
//document.getElementById("tr").appendChild(document.createElement("td")); //creates table data for the row (only one)dd
//nodeArray.push(nodeList[i].childNodes[0].nodeValue); //adds to array the lines participating in quiz
//document.getElementById("td").appendChild(createTextNode(nodeList[i],childNodes[0].nodeValue));
}
}
main.appendChild(para1); //add paragraph to the main div
main.appendChild(para2); //adds 2nd paragraph to main div
}
所以我在if语句中注释了所有其他内容,但是仍然出现此错误...如果我早先已经创建了表标签,为什么将其视为空?
So I have commented everything else out in the if statement but I'm still getting this error...if I already created the table tag earlier, why is it considered null??
推荐答案
将代码更改为:
var table = para2.appendChild(document.createElement("table"));
var tbody = table.appendChild(document.createElement('tbody'));
现在将行添加到 tbody
.其他答案告诉您有关 getElementById
错误的信息.您问题的注释告诉您为什么要创建TBODY元素.
now add rows to tbody
. Other answers tell you about the getElementById
error. The comment to your question tells you why to create a TBODY element.
这篇关于“无法将方法'appendchild'调用为null" ...错误...为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!