为数组值添加换行符

adding line breaks to array values javascript

本文关键字:换行符 添加 数组      更新时间:2023-09-26

这是我的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(<'br>),但这打破了每一个单词,这是我的代码:

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);
}
如果需要的话,我会尽我最大的努力发布更多的信息。我确实使用了这个片段,我是Javascript的新手。我不反对自学,但我在网上找遍了,却找不到我的答案。

要制作array of Strings,最好将complete string放在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;     
 }

这是我采用的方法,使用.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;

这里有一个小提琴:http://jsfiddle.net/brettwlutz/Q35J2/1/

我认为数组是这样的:

var arr = [val1, val2, val3];

可以使用arr。Push添加更多的值或arr。禁止将值添加到数组的开头

http://jsfiddle.net/h_awk/K3kEv/

<script>
var arr = [1, 2, 3, 4], i;
for( i=0; i<arr.length; i++ )
{
document.write(arr[i] + '<br />');
}
</script>

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

我的问题是,为什么你从一个只有一个元素的数组开始?一个字符串可以被分割成一个数组,并以同样的方式连接回一个字符串。另外,与其使用波浪号~来分隔要分割的语句,还不如使用一种合适的数组语法,然后去掉"split",只使用连接符

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,这只是一个学习如何分割数组的例子,但这并不是一个真正的现实生活中的应用程序,这就是为什么这篇文章对Stack Overflow用户来说似乎是有问题的。