Javascript,未捕获的类型错误:无法读取未定义的属性“单元格”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33881227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:59:58  来源:igfitidea点击:

Javascript, Uncaught TypeError: Cannot read property "cells" of undefined

javascripthtmljsondynamic

提问by nuclearmage

I'm trying to work with a table in Javascript.

我正在尝试使用 Javascript 中的表格。

I have a function to create an empty table,

我有一个函数来创建一个空表,

//This function will create an empty table of a length of vars+1
//This function will create the header slots as well as empty data slots
function empty_Table(){
        document.write("<table border=\"1\" id=\"myTable\" >");
        var my_Structure = steps_Structure(steps);
        var vars = my_Structure[0].second.split(",");//This will create an array,str, of multiple variables
        //first row
        document.write("<TR>");
        for (var i = 0; i<vars.length+2; i+=2){
                document.write("<TH></TH>");
        }
        document.write("</TR>");
        //second row
        document.write("<TR>");
        for (var i = 0; i<vars.length+2; i+=2){
                document.write("<TD></TD>");
        }
        document.write("</TR>");
        //third row
        document.write("<TR>");
        for (var i = 0; i<vars.length+2; i+=2){
                document.write("<TD></TD>");
        }
        document.write("</TR>");
        document.write("</Table>");
        return;
}

and a function to modify that table,

以及修改该表的函数,

//This function is to begin drawing the variable display table
//This function should create a dynamic table that grows depending on the values in steps
function draw_Table(){
        var my_Structure = steps_Structure(steps);
        empty_Table();
        var myTable = document.getElementById('myTable');
        //draw first row
        var vars = my_Structure[0].second.split(",");//This will create an array, of multiple variables

        myTable.rows[0].cells[0].innerHTML = 'Variables:';
        //for (var i = 0; i<vars.length; i+=2){
                //document.write("<TH>",vars[i],"</TH>");
        //}

        //draw second row
        var pairs = my_Structure[step_Cur].second.split(",");                                                                                  
        myTable.rows[1].cells[0].innerHTML = 'Current Value:';
        //for (var i = 1; i<(pairs.length); i+=2){
                //document.write("<TD>",pairs[i],"</TD>");
        //}
        //third row                                                                                                    
        myTable.rows[2].cells[0].innerHTML = 'Last Value:';
}

3 lines are giving me trouble

3 条线给我带来了麻烦

myTable.rows[1].cells[0].innerHTML = 'Variables:';
myTable.rows[2].cells[0].innerHTML = 'Current Value:';
myTable.rows[3].cells[0].innerHTML = 'Last Value:';

In chrome, I'm seeing the error "Uncaught TypeError: Cannot set property 'innerHTML' of undefined" but have no idea how to fix it.

在 chrome 中,我看到错误“Uncaught TypeError: Cannot set property 'innerHTML' of undefined”,但不知道如何修复它。

I am not a javascript user -- I was just given an assignment to write a tool for displaying the steps in solving a function -- to this regard, I have very little javascript knowledge and am seriously struggling with this table

我不是 javascript 用户——我刚刚接到了一个任务来编写一个工具来显示求解函数的步骤——在这方面,我对 javascript 的了解很少,并且正在认真地处理这张表

How it ~should~ work: Given list of variables and their values make a table of 3 rows, number of columns corresponding to amount of variables passed.

它应该如何工作:给定变量列表及其值,制作一个 3 行的表格,列数对应于传递的变量数量。

   Variables:|   a  |   b  |  c
 Current Val:| val1 | val2 | val3
previous val:| val1 | val2 | val3

where previous val shows the values of the previous step of the function, current val shows the current values of each of the variables of the function, and variables gives variables.

其中previous val 显示函数上一步的值,current val 显示函数每个变量的当前值,variables 给出变量。

The variables and headers are constant upon table creation, the values in the table will change with each time a "next"/"back" button is pressed to display the next/previous step.

变量和标题在表格创建时是不变的,表格中的值会随着每次按下“下一步”/“后退”按钮以显示下一步/上一步而改变。

Thank you for any help you can provide!

感谢您提供任何帮助!

EDIT: Found the bugs with the help of this great community -- The "cells" of undefined bug was caused by an off-by-one subscript, the original bug was caused by creating my table wrong. I closed <TR>before I actually added data to it.

编辑:在这个伟大社区的帮助下发现了错误——未定义错误的“单元格”是由一个不合一的下标引起的,原始错误是由我的表创建错误引起的。我<TR>在实际添加数据之前关闭了它。

回答by Hristo Borisov

One thing I see is that you don't construct your table correctly:

我看到的一件事是您没有正确构建表格:

        //first row
        document.write("<TR id=></TR>");
        for (var i = 0; i<vars.length+2; i+=2){
                document.write("<TH></TH>");
        }

here you create a row and after that you create a cell. Then, when you get the table row, it is empty which is actually true.

在这里创建一行,然后创建一个单元格。然后,当您获得表格行时,它是空的,这实际上是正确的。

What you need is something like this:

你需要的是这样的:

document.write("<TR>");
for (var i = 0; i<vars.length+2; i+=2){
     document.write("<TH></TH>");
}
document.write("</TR>");

this way the table row will have the cells.

这样表格行就会有单元格。

Let me know if this helps you.

如果这对您有帮助,请告诉我。