简体   繁体   English

如何在字符串变量中将行追加到表

[英]How append row to table in string variable

I need to create a table by string variable. 我需要通过字符串变量创建一个表。
I want append row to string by format of tabs. 我想按标签格式将行追加到字符串。
How do you do that? 你是怎样做的?

var textSummary = "DATE              USER                COMMENT               ";

How add row to string and keep on this template? 如何将行添加到字符串并保持在此模板上?

I am not clear on what you are trying to accomplish but from what I understand you are trying to create table row entries using string variables. 我不清楚您要完成什么,但是据我了解,您正在尝试使用字符串变量创建表行条目。 Take a look at this code which dynamically creates table row elements and creates table entries using string variables: 看一下这段代码,该代码动态创建表行元素并使用字符串变量创建表条目:

var table = document.createElement('table');
var numOfRows = 5
for (var i = 1; i <= numOfRows - 1; i++){
    var tr = document.createElement('tr');   

    var td1 = document.createElement('td');
    var td2 = document.createElement('td');
    var td3 = document.createElement('td');

    var textSummary1 = document.createTextNode('Date' + i);
    var textSummary2 = document.createTextNode('User' + i);
    var textSummary3 = document.createTextNode('Comment' + i);


    td1.appendChild(textSummary1);
    td2.appendChild(textSummary2);
    td3.appendChild(textSummary3);

    tr.appendChild(td1);
    tr.appendChild(td2);
    tr.appendChild(td3);

    table.appendChild(tr);
}
document.body.appendChild(table);

It creates 4 rows with 3 columns each for Date, User, Comments. 它创建4行,每行3列,分别用于日期,用户,注释。 you can change the number of rows by changing the numOfRows variable in the loop. 您可以通过更改循环中的numOfRows变量来更改行数。 You can change the parameters for document.createTextNode to whatever your data is. 您可以将document.createTextNode的参数更改为任何数据。

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

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