简体   繁体   English

使用来自字符串数组的字符填充HTML表

[英]Populate an HTML table with characters from String Array

I need to create a 2*50 cell table and populate it with string literal pre-stored in a JavaScript array. 我需要创建一个2 * 50的单元格表,并用预先存储在JavaScript数组中的字符串文字填充它。

I have stored the string in an array and created a table. 我已经将字符串存储在数组中并创建了一个表。 I am unable to populate it. 我无法填充它。

 //string literal pre-stored in a javascipt array
 var proverbs = ["Love sees no faults", "friend in need is a friend indeed"];
 var txt= proverbs[0];

 document.write('<table id= "tab" border="1" cellspacing="1" cellpadding="30">')
 for(i = 0; i < 50; i++){
    document.write('<tr>')
    document.write('<td>')
    document.write('<td>')
    document.write('</tr>')
 }

 for (i = 0; i < txt.length; i++) {
   tab +=  document.write("<tr><td>" + txt[i] + "</td></tr>");
 } 

 document.write('</table>')

This does not work. 这是行不通的。 Ho w do I now populate the table. 我现在该如何填充表格。 The last for loop I wrote is to fill up the table cells, one char per cell. 我写的最后一个for循环是填充表单元格,每个单元格一个字符。 Can anyone tell how do I implement it. 谁能告诉我如何实现它。

Your code looks like it's missing a few things. 您的代码似乎缺少了一些东西。

1st problem: var proverbs = ["Love sees no faults", "friend in need is a friend indeed"]; 第一个问题:var proverbs = [“爱无可挑剔”,“需要的朋友的确是朋友”]; var txt= proverbs[0]; var txt =谚语[0];

You assign proverbs sub 0 to txt. 您将箴言子0分配给txt。 That's fine, the value of txt is now "Love sees no faults". 很好,txt的值现在是“爱没有错”。 But, later you reference txt[i]. 但是,稍后您将引用txt [i]。

2nd problem 第二个问题

for (i = 0; i < txt.length; i++) {
  tab +=  "<tr><td>" + txt[i] + "</td>";
}

you don't close the tr tag. 您不会关闭tr标签。 But then also you don't do anything with tab. 但是,您也不会对tab执行任何操作。

I think you are printing an empty table. 我认为您正在打印一个空表。

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

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