我正在尝试在数组中的每个项目之间进行换行。我的程序要做的是根据用户对问题的回答,使用其他几个数组创建一个新数组。我似乎无法弄清楚如何在每个项目之间允许换行。
所有操作都用JavaScript完成(除了一些CSS之外,但并不重要)。我尝试使用
标签,但只会打印
。
我有四个相关的数组。 一个问题:
var questions = ["Do you like volunteering","Would you be interested in joining the musical/theatre?","Would you..."] //it goes on and on but I'll spare you
另一个可能的结果:
var clubs=[["All In","Interact Club","Key Club"," Environmental Club"],[" Fall Production"," Spring Musical"], "Student Government"," Tricorn"] //also goes on for a while
一个称为recClubs的空数组,在他们做出选择时会被填充。 还有一个空数组,称为选项本身。
然后再打印最终结果:
function endQuizDiv(){
//header of screen
document.getElementById("question").innerHTML= "These are the clubs that you might be interested in";
//prints the recommended clubs
document.getElementById("recClubs").innerHTML= recClubs;
}
这是recClubs获取信息的地方:
function eliminateClubs(){
for(h=0;h<=personChoices.length;h++){
if (personChoices[h]==1){
recClubs.push(clubs[h]);
}
}
}
将recClubs提供给用户时,所有俱乐部都在一个文本块中,并用逗号分隔。我希望它们之间用换行符分隔。任何帮助表示赞赏。
答案 0 :(得分:1)
您可以对数组recClubs.push(clubs[h].join(', <br/>'));
使用join,实际上您是在将数组添加到innerHtml中,但是它可能是带有<br/>
标签的格式化字符串
但是数据结构应该是:
var clubs=[["All In","Interact Club","Key Club"," Environmental Club"],[" Fall Production"," Spring Musical"], ["Student Government"],[" Tricorn"]].
或不更改数据结构
function eliminateClubs(){
for(h=0;h<=personChoices.length;h++){
if (personChoices[h]==1){
var addedValue = Array.isArray(clubs[h]) ? clubs[h].join(', <br/>') :clubs[h] + '<br/>';
recClubs.push(addedValue);
}
}