简体   繁体   English

Javascript .createElement & .appendChild 问题(包括 for 和 if)

[英]Javascript .createElement & .appendChild Issue (Includes for and if)

I am a pretty novice javascript user.我是一个非常新手的 javascript 用户。 I need some help debugging this code.我需要一些帮助来调试这段代码。 What I intend this to do is based on the amount of values in the list, is create a 3 column wide table to display each.我打算这样做是基于列表中值的数量,是创建一个 3 列宽的表来显示每个值。 BTW the whole html formatting is set up with grid.顺便说一句,整个 html 格式都是用网格设置的。

The error is: Uncaught TypeError: Cannot read properties of undefined (reading 'appendChild')错误是: Uncaught TypeError: Cannot read properties of undefined (reading 'appendChild')

HTML (inside body): HTML(内部):

<section id="db_gallery">
      <table id="gallery_table"></table>
      <script src="autogallery.js"></script>
</section>

JS in autogallery.js: autogallery.js 中的 JS:

const gallery_table = document.getElementById("gallery_table");
const list = ["A", "B", "C", "D", "E", "F"];

for (let i of list) {
    if (i % 3 === 0) {
        let newRowItem = document.createElement("tr");
        var idName = "newRowItem";
        idName.concat(i);
        newRowItem.setAttribute("id", idName);
        gallery_table.appendChild(newRowItem);
    }

    let newColItem = document.createElement('th');
    newColItem.textContent = i;

    idName.appendChild(newColItem);
    console.log(idName);
}

Also it would be a big help if any suggestions were simple to understand.如果任何建议易于理解,那将是一个很大的帮助。 If it means anything I will eventually be linking this to a phpmyadmin database as the values in the array.如果这意味着什么,我最终会将其链接到 phpmyadmin 数据库作为数组中的值。 Thanks!谢谢!

First you should newRowItem.appendChild instead of idName because newRowItem is the element you've created.首先你应该newRowItem.appendChild而不是idName因为newRowItem是你创建的元素。

And second when using for...of i is the element not the index, so it's better to use for in your case.其次,当使用for...of i时,元素不是索引,因此最好在您的情况下使用for

And last you shouldn't use newRowItem outside the scope because you declared it with let inside if caluse.最后,您不应该在范围之外使用newRowItem ,因为您使用let inside if caluse 声明了它。

this should be correct:这应该是正确的:

const gallery_table = document.getElementById("gallery_table");
let list = ["A", "B", "C", "D", "E", "F"];
var idName = "";

    for (let i = 0; i < list.length; i++) {
        if (i % 3 === 0) {
            let newRowItem = document.createElement("tr");
            idName = "newRowItem";
            idName = idName.concat(list[i]);
            newRowItem.setAttribute("id", idName);
            gallery_table.appendChild(newRowItem);
            let newColItem = document.createElement('th');
            newColItem.textContent = list[i];
            newRowItem.appendChild(newColItem);
        }
    }

Try尝试

newRowItem.appendChild(newColItem)

instead of代替

idName.appendChild(newColItem)

Use for loop with index to do modulo operation on list indexes.使用带索引的 for 循环对列表索引进行模运算。

    let newRowItem;
    for (int i=0 ; i<list.length; i++) {
        if (i % 3 === 0) {
            newRowItem = document.createElement("tr");
            var idName = "newRowItem";
            idName.concat(i);
            newRowItem.setAttribute("id", idName);
            gallery_table.appendChild(newRowItem);
        }
        if(newRowItem){
            let tag = i < 3 ? 'th' : 'td';
            let newColItem = document.createElement(tag);
            newColItem.textContent = list[i];
            newRowItem.appendChild(newColItem);
            console.log(newRowItem);
        }
   }`

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

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