从逗号分隔的字符串创建HTML表格javascript [英] Create html table from comma separated strings javascript
问题描述
< table>
< tr>< td>< u>学校< / u>< / td>
< td>< u>百分比< / u>< / td>
< tr>< td> School 1:< / td>
< td> Percent1< / td>< / tr>
< tr>< td> School 2:< / td>
< td> Percent2< / td>< / tr>
< tr>< td> School 3:< / td>
< td> Percent3< / td>< / tr>
< / table>
我输入的是逗号分隔的字符串:
var school_list =School 1,School 2,School 3
var pct_list =Percent1,Percent2,Percent3
该函数需要传入school_list和pct_list,并返回上面的html表代码的字符串。
var schoolArr = school_list.split(class =h2_lin>解决方案
'');
var pctArr = pct_list.split(',');
var table =< table>; (var i = 0; i< schoolArr.length; i ++){
table = table +< tr>< td>+ schoolArr [i] +< / td> < td>+ pctArr [i] +< / td>< / tr>;
}
table = table +< / table>;
返回表;
I am trying to write a Javascript function which writes the text to (eventually) create the following html tables (I will be passing different length arguments to it to create hundreds of tables):
<table>
<tr><td><u>School</u></td>
<td><u>Percent</u></td>
<tr><td>School 1: </td>
<td>Percent1</td></tr>
<tr><td>School 2: </td>
<td>Percent2</td></tr>
<tr><td>School 3: </td>
<td>Percent3</td></tr>
</table>
The inputs that I have are comma separated strings:
var school_list = "School 1, School 2, School 3"
var pct_list = "Percent1, Percent2, Percent3"
The function needs to be passed school_list and pct_list, and return a string of the html table code above.
Something like this:
var schoolArr = school_list.split(',');
var pctArr = pct_list.split(',');
var table = "<table>";
for (var i=0; i< schoolArr.length; i++) {
table = table + "<tr><td>"+ schoolArr[i]+"</td><td>"+ pctArr[i] +"</td></tr>";
}
table = table + "</table>";
return table;
这篇关于从逗号分隔的字符串创建HTML表格javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!