Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

javascript - Java脚本:TypeError:动态打印表时无法读取null的属性'createTHead'(Java Script: TypeError: Cannot read property 'createTHead' of null, while printing table dynamically)

I am a beginner at coding and I am trying to print a table dynamically in my HTML file.

(我是编码的初学者,我正在尝试在HTML文件中动态打印表格。)

The following error appears: "Uncaught TypeError: Cannot read property 'createTHead' of null at generateTableHead3", where generateTableHead3 is my calling of function to print the header.

(出现以下错误:“未捕获的TypeError:在generateTableHead3处无法读取null的'createTHead'属性”,其中generateTableHead3是我调用的函数,用于打印标题。)

I have been trying to understand why this is happening to absolutely no avail for the last 4 hours.

(我一直在试图了解为什么最近4个小时内这种情况绝对无效。)

I have other printed tables dynamically, and they all work.

(我有其他动态打印的表,它们都可以工作。)

This code is copy-pasted from the original table which prints perfectly on the HTML, and so do other 2 tables created in the same JS file.

(该代码是从原始表中复制粘贴的,该表可以完美地在HTML上打印,在同一JS文件中创建的其他2个表也是如此。)

And then, the third one doesn't.

(然后,第三个没有。)

I created a separate html and js to test, and it still returns the error.

(我创建了单独的html和js进行测试,但仍然返回错误。)

What am I missing?

(我想念什么?)

let header3 = ["Name", "No. Party", "% Party Votes"];

function generateTableHead3(table, array) {

    let thead = table.createTHead();
    let row = thead.insertRow(0);
    for (i = 0; i < array.length; i++) {
        let cell = document.createElement("th");
        cell.innerHTML = array[i];
        row.appendChild(cell);
    }
}
function generateTable3(table, data) {
    for (let element of data) {

        let row = table.insertRow();
        let cell1 = row.insertCell();
        let a = document.createElement('a');
        let link = document.createTextNode(element['first_name'] + ' ' + (element['middle_name'] || '') + ' ' + element['last_name']);
        a.appendChild(link);
        a.href = element.url;
        a.title = link;
        document.body.appendChild(a);
        cell1.appendChild(a);


        }
    }

let table3 = document.getElementById("least-loyal");
let data3 = Object.keys(array10LeastLoyal);
generateTableHead3(table3, header3);
generateTable3(table3, array10LeastLoyal);
  ask by Nelfo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...