Javascript 向数组值添加换行符

Javascript 向数组值添加换行符,javascript,Javascript,以下是我的javascript数组: var quizArray = [ 'When the weather is agreeable what do you prefer to do the most?~Something outside...Obviously!~I tend to enjoy things that aren\'t dependent on weather.~Read, possibly outside if I can find my sunscreen.~Do what

以下是我的javascript数组:

var quizArray = [
'When the weather is agreeable what do you prefer to do the most?~Something outside...Obviously!~I tend to enjoy things that aren\'t dependent on weather.~Read, possibly outside if I can find my sunscreen.~Do what I always do, which is whatever I want.~Try something new, like Planking.~~~','It\'s a weeknight and friend invites you to an orchestra. You would?~Kindly refuse. It\'s just not my thing.~Go, unquestionably. I love all art forms.~Ask who the composer is, then read all about them before going.~Confuse Orchestra with Opera and begin singing in Latin.~Go if the tickets are free, otherwise no.~~~',]
当我加载html时,它不会在每个答案后显示换行符。我尝试在拆分后添加一个
.join()
,但这会分解每个单词,下面是我的代码:

function displayQuiz(ent, qnum) {

perPage++;
var qna = quizArray[qnum].split('~');
var od = []; for (var i = 1; qna[i] != null && qna[i] != ''; i++) od.push(i); od.sort( randOrd ); od.sort( randOrd ); 
var newF = document.createElement("form"); 
var newDq = document.createElement("div"); 
newDq.className = 'question'; 
newDq.appendChild(document.createTextNode(Number(qnum+1)+ ': ' +qna[0])); 
newF.appendChild(newDq); 
newDq = document.createElement("div"); 
newDq.className = 'answers'; 
for (var i = 1; qna[i] != null && qna[i] != ''; i++) {var newDa = document.createElement("label"); newDa.htmlFor = 'a'+qnum+i; /*@cc_on @if (@_jscript) var newR = document.createElement("<input name='a"+qnum+"'>"); @else */ 
var newR = document.createElement("input"); 
newR.name = 'a'+qnum; /* @end @*/ 
newR.type = 'radio'; 
newR.id = 'a'+qnum+i; 
newR.value = od[i-1]; 
newDa.appendChild(newR); 
newDa.appendChild(document.createTextNode(' '+qna[od[i-1]]+' ')); 
newDq.appendChild(newDa);} 
newF.appendChild(newDq); 
document.getElementById('quiz'+perPage).appendChild(newF);
}
函数显示测验(ent,qnum){
perPage++;
var qna=quizArray[qnum]。拆分(“~”);
变量od=[];for(变量i=1;qna[i]!=null&&qna[i]!='';i++)od.push(i);od.sort(randOrd);od.sort(randOrd);
var newF=document.createElement(“表单”);
var newDq=document.createElement(“div”);
newDq.className='question';
newDq.appendChild(document.createTextNode(Number(qnum+1)+':'+qna[0]);
newF.appendChild(newDq);
newDq=document.createElement(“div”);
newDq.className='answers';
对于(var i=1;qna[i]!=null&&qna[i]!='';i++){var newDa=document.createElement(“标签”);newDa.htmlFor='a'+qnum+i;/*@cc_on@if(@_jscript)var newR=document.createElement(“”;@else*/
var newR=document.createElement(“输入”);
newR.name='a'+qnum;/*@end@*/
newR.type='radio';
newR.id='a'+qnum+i;
新值=外径[i-1];
newDa.appendChild(newR);
newDa.appendChild(document.createTextNode(“”+qna[od[i-1]]]+“”));
newDq.appendChild(newDa);}
newF.appendChild(newDq);
document.getElementById('quick'+每页)。appendChild(newF);
}

如果需要,我会尽力发布更多信息。我确实将此作为一个片段使用,我对Javascript非常熟悉。我并不反对自己学习,但我已经浏览了互联网,找不到我的答案。

我认为数组是这样制作的:

var-arr=[val1,val2,val3]

可以使用arr.push追加更多值,或使用arr.unshift将值添加到数组的开头


var arr=[1,2,3,4],i;

对于(i=0;i要生成字符串的
数组
最好将
完整字符串
放入
var
中,然后进行
split()
,对于add
可以使用
join()
for()

最好这样写代码

var quizArray = 'When the weather is agreeable what do you prefer to do the most?~Something outside...Obviously!~I tend to enjoy things that aren\'t dependent on weather.~Read, possibly outside if I can find my sunscreen.~Do what I always do, which is whatever I want.~Try something new, like Planking.~~~';

        function displayQuiz(ent, qnum) {

            perPage++;
            var qna = quizArray.split('~');
            var res = qna.join(" <br> ");
            return res;     
 }
var quizArray='当天气宜人时,你最喜欢做什么?~户外活动……显然!~我喜欢不依赖天气的活动。~阅读,如果我能找到防晒霜,可能在户外。~做我经常做的事,这是我想要的。~尝试一些新的东西,比如木板。~~~;
函数显示测验(ent,qnum){
perPage++;
var qna=quizArray.split(“~”);
var res=qna.join(“
”); 返回res; }
以下是我采用的方法,使用.join添加br元素。如果在每个单词后添加br,我认为您没有指定最初拆分的内容

var string = 'When the weather is agreeable what do you prefer to do the most?~Something 
outside...Obviously!~I tend to enjoy things that aren\'t dependent on weather.~Read, possibly outside if I can find my sunscreen.~Do what I always do, which is whatever I want.~Try something new, like Planking.~~~';

var quizArray = string.split('~');
var finalString = quizArray.join('<br/>');

document.getElementById('yourIdHere').innerHTML = finalString;
var string='当天气宜人时,你最喜欢做什么?~什么
户外……显然!~我喜欢不依赖天气的东西。~阅读,如果我能找到防晒霜,可能就在户外。~做我一直在做的事,这是我想要的。~尝试一些新的东西,比如铺板。~~~;
var quizArray=string.split('~');
var finalString=quizArray.join(“
”); document.getElementById('yourIdHere')。innerHTML=finalString;

下面是一个小问题:

首先,回答您的问题。上面的代码应该可以工作,并使变量qna包含新的拆分数组。然后,添加.join(
)的解决方案应该将该新数组转换为带有html换行符的单个字符串。也就是说,除非您希望使用t JS换行符,在这种情况下,您应该使用.join(“\n”)

我的问题是,为什么要从只有一个元素的数组开始?一个字符串可以拆分为一个数组,然后以相同的方式重新连接为一个字符串。另外,可能更容易的方法是,不使用tilde~来分隔要拆分的语句,只需使用一种正确的数组语法形式,然后去掉“拆分”只需使用连接:

var quizArray = ["When the weather is agreeable what do you prefer to do the most?", "Something outside...Obviously!, I tend to enjoy things that aren\'t dependent on weather.", "Read, possibly outside if I can find my sunscreen.", "Do what I always do, which is whatever I want.", "Try something new, like Planking."];

我唯一可能的理解是,您仍在学习JS,这只是一个学习如何拆分数组的示例,但这并不是一个真正的应用程序,这就是为什么这篇文章对堆栈溢出用户来说似乎有问题。

您有一个带有单个字符串的数组。您应该只分配一个字符串,然后拆分数组字符串到数组中。没有意义的是为什么要使用
~
字符而不是换行符(
\n
)。这是对文本布局引擎的提示,提示可以换行。除非执行
arr.join('\n')
“这将在每个逗号后自动换行”不,不是。数组只是具有特殊长度属性和方便方法的对象,文本语法和默认字符串表示使用逗号分隔值。并且由于OP极不可能使用XHTML,因此答案应该使用HTML语法:
arr.join(“
”);
您应该解释您的答案,而不仅仅是发布代码,并提供一个使用示例。这可以在一行中完成:
quizArray.split(“~”).join(“
”)
。如果我这样做,我会在每个字母旁边用单选按钮将其隔开。我发布了我的其余代码,这样也许有人可以帮我回答。谢谢。好吧,我理解你的意思。不过,在这个数组中有20多个问题。我只是将它们全部删掉,以便于回答。
var quizArray = ["When the weather is agreeable what do you prefer to do the most?", "Something outside...Obviously!, I tend to enjoy things that aren\'t dependent on weather.", "Read, possibly outside if I can find my sunscreen.", "Do what I always do, which is whatever I want.", "Try something new, like Planking."];