简体   繁体   English

无法调用方法appendChild为null

[英]Cannot call method appendChild of null

It occurs in this: 它出现在:

for(var i = 0; i<phoneNums.length; i++)
{
    var lines = document.createElement('tr');
    var number = document.createElement('td');
    number.innerHTML = phoneNums[i];
    var minutes = document.createElement('td');
    minutes.innerHTML = minutesUsed[i];
    var plan = document.createElement('td');
    plan.innerHTML = plans[i];
    var charges = document.createElement('td');
    charges.innerHTML = charge[i];
    document.getElementById('theTable').appendChild(lines);
    lines.appendChild(number);
    lines.appendChild(minutes);
    lines.appendChild(plan);
    lines.appendChild(charges);
}

Specifically at: 具体在:

document.getElementById('theTable').appendChild(lines);

<table id = 'theTable'>
        </table>
        <script type = 'text/javascript'>
            populateTable();
        </script>

I'm trying to create a function that adds tr and td and the values to a table. 我正在尝试创建一个将trtd以及值添加到表中的函数。

You probably are running this script before "theTable" exists in your document. 您可能在文档中存在“theTable”之前运行此脚本。 Make sure that this script occurs below your table with id="theTable" , or that it is run in an event handler that occurs after the table exists. 确保此脚本出现在具有id="theTable"表下方,或者它是在表存在后发生的事件处理程序中运行的。

This means that you do not have an element in your DOM with an id of "theTable". 这意味着您的DOM中没有ID为“theTable”的元素。 Somewhere in your html you should have something like: 你的HTML中的某个地方你应该有类似的东西:

<table id="theTable">...

Alternatively, as others have also mentioned, this element may not have been loaded into the DOM before your script executes. 或者,正如其他人也提到的那样,在脚本执行之前,可能尚未将此元素加载到DOM中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM