简体   繁体   English

如何使用 getElementByID 迭代并制作表格

[英]How to iterate through and make a table using getElementByID

function myFunction() {
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  cell1.innerHTML = "NEW CELL1";
  cell2.innerHTML = "NEWCELL2";
  for (i = 0; i < 5; i++) {
     row.insertCell(i);
     row.insertRow(0)
     i.innerHTML = "Proof this is Working"
   }
}

I apologize, as I am very new to Javascript and it's my second language.我很抱歉,因为我对 Javascript 很陌生,它是我的第二语言。

So I have this function, and using a variable works for inserting the cell/row into an HTML Table.所以我有这个功能,并使用变量将单元格/行插入到 HTML 表中。 However, I am trying to make a grid from a table, so I need to have a for loop that inserts cells and rows, as I will need a lot of them.但是,我正在尝试从表格中创建一个网格,因此我需要有一个 for 循环来插入单元格和行,因为我将需要很多它们。 So the first few lines, var cell1 and var cell2 work perfectly, and make cells and rows.所以前几行, var cell1 和 var cell2 完美地工作,并制作单元格和行。 However the for loop is not working correctly at all.但是 for 循环根本无法正常工作。

I checked syntax, and it appears to look fine.我检查了语法,看起来不错。 the code looks like it should be working, but this is my first time writing a for loop in javascript.代码看起来应该可以工作,但这是我第一次在 javascript 中编写 for 循环。

Another thing I have to ask, does a for loop have to have three conditions?我要问的另一件事是,for 循环是否必须具有三个条件? In python you can just do a for line in x, and you should be fine.在 python 中,你可以在 x 中做一个 for 行,你应该没问题。 But it looks like in javascript you need three different conditions.但看起来在 javascript 中你需要三个不同的条件。

Thank you!谢谢!

You made a couple major errors in this block of code:您在此代码块中犯了几个主要错误:

  for (i = 0; i < 5; i++) {
     row.insertCell(i); 
     row.insertRow(0); 
     i.innerHTML = "Proof this is Working";
   }

Line by line:逐行:

row.insertCell(i);

This insert a new cell to the row you had defined before the for loop.这会向您在 for 循环之前定义的行插入一个新单元格。 It will overwrite the first two cells you defined before the for loop.它将覆盖您在 for 循环之前定义的前两个单元格。

     row.insertRow(0); 

I am not sure what this does, but it should either throw an error or produced malformed html.我不确定这是做什么的,但它应该抛出错误或生成格式错误的 html。 You want to insert a new row to the table not to the already existing row.您想向表中插入新行而不是已存在的行。 table.insertRow(0) will overwrite the first row you defined before the for loop. table.insertRow(0)将覆盖您在 for 循环之前定义的第一行。

 i.innerHTML = "Proof this is Working";

i is an integer it should not have the property .innerHTML and will probably throw an error. i是一个整数,它不应该具有.innerHTML属性,并且可能会引发错误。

It is a bit hard to understand exactly your intent.要准确理解您的意图有点困难。 If your intent is to add 5 additional rows with 5 cells you can do that like this:如果您的意图是添加 5 个额外的行和 5 个单元格,您可以这样做:

function myFunction() {
      var table = document.getElementById("myTable");
      var row = table.insertRow(0);
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      cell1.innerHTML = "NEW CELL1";
      cell2.innerHTML = "NEWCELL2";
      for (let i = 0; i < 5; i++) {
         let newRow = table.insertRow(-1)
         for(let j = 0; j < 5; j++) {
             newRow.insertCell(-1)
         }
       }
    }

newRow.insertCell(-1) the -1 adds the new cell to the end of the row https://www.w3schools.com/jsref/met_tablerow_insertcell.asp newRow.insertCell(-1) -1将新单元格添加到行的末尾https://www.w3schools.com/jsref/met_tablerow_insertcell.asp

And finally yes the syntax for the for loop looks fine, there are other ways of iterating in javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration最后是的 for 循环的语法看起来不错,还有其他方法可以在 javascript 中进行迭代: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

There are a few thing wrong with your current code.您当前的代码有一些问题。

  1. row.insertRow(0) You can't insert rows into rows. row.insertRow(0)不能在行中插入行。
  2. i.innerHTML = "Proof this is Working" i is the variable that holds the current index of the loop. i.innerHTML = "Proof this is Working" i是保存当前循环索引的变量。 An integer doesn't have the property innerHTML .整数没有属性innerHTML You probably want to set the contents of the cell.您可能想要设置单元格的内容。 In which case you first have to save the cell into a variable.在这种情况下,您首先必须将单元格保存到变量中。 const cell = row.insertCell(i) then cell.innerHTML = "Proof thiis is working" . const cell = row.insertCell(i)然后cell.innerHTML = "Proof thiis is working"

Here is an example of how things might look:下面是一个示例,说明事情的样子:

 function myFunction() { const table = document.getElementById("myTable"); for (let rowNr = 0; rowNr < 7; ++rowNr) { const row = table.insertRow(); for (let colNr = 0; colNr < 3; ++colNr) { const cell = row.insertCell(); cell.innerHTML = `row ${rowNr} - column ${colNr}`; } } } myFunction();
 td { border: 1px solid black; }
 <table id="myTable"></table>

Another thing I have to ask, does a for loop have to have three conditions?我要问的另一件事是,for 循环是否必须具有三个条件? In python you can just do a for line in x, and you should be fine.在 python 中,你可以在 x 中做一个 for 行,你应该没问题。 But it looks like in JavaScript you need three different conditions.但看起来在 JavaScript 中你需要三个不同的条件。

A for-loop does not have to have three conditions. for 循环不必具有三个条件。 Here are the different types of for-loops:以下是不同类型的 for 循环:

  1. for The is the version you where currently using. for是您当前使用的版本。 This is a general purpose for-loop with 3 sections: initialisation, condition and finalisation.这是一个通用的 for 循环,包含 3 个部分:初始化、条件和结束。

  2. for...in This version is mainly used to loop over properties of an object. for...in这个版本主要用于循环对象的属性。

  3. for...of This version is mainly used to loop over the elements of iterable objects like arrays, sets, maps. for...of这个版本主要用于遍历可迭代对象的元素,如数组、集合、映射。 This loop comes the closest to the one used in Python.这个循环最接近 Python 中使用的循环。

    An example might be:一个例子可能是:

     for (const line of lines) console.log(line);
  4. for await...of Similar to for...of but relevant for asynchronous programming. for await...of类似于for...of但与异步编程相关。 Don't worry about this one for the moment.暂时不要担心这个。

The question I have is are you trying to insert 5 rows or 5 cells?我的问题是您要插入 5 行还是 5 个单元格? Here's what I'd do:这是我要做的:

// insert a single row given the cells as an array
function insertRow( table, cells ) {
    const row = table.insertRow(-1); // append a row at the bottom of the table
    for( var i = 0; i < cells.length; i++ ) {
        const cell = row.insertCell( i );
        cell.innerHtml = cells[i];
    }
}

// insert a series of rows given the input array
function insertRows( table, rows ) {
    for( var i = 0; i < rows.length; i++ ) {
        insertRow( table, rows[i] );
    }
}

function main() {
    const table = document.getElmentById('#someTable');

    insertRows( table, [
       [ 'Moon Landing', 'July 20, 1969', 'Neil Armstrong' ],
       [ 'Hoover Dam', 'Sept 30, 1935', 'Franklin D. Roosevelt' ],
       [ 'ARPANET First Packet', 'Oct 29, 1969', 'UCLA,UC Berkley' ]
    ] );
}

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

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