将换行符添加到数组值javascript

时间:2014-05-02 21:46:36

标签: 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 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的新手。不反对自己学习,但是我已经倒在了互联网上,找不到我的答案。

4 个答案:

答案 0 :(得分:3)

如果您将array of Strings放在complete string中并且在制作var之后将split()做得更好,并且为了添加,您可以使用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;     
 }

答案 1 :(得分:1)

我认为阵列是这样的:

var arr = [val1,val2,val3];

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

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>

答案 2 :(得分:0)

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

答案 3 :(得分:0)

首先,回答你的问题。上面的代码应该工作,并使变量qna包含新的split数组。您添加.join(“
”)的解决方案应该将该新数组转换为带有html换行符的单个字符串。也就是说,除非你想要JS换行,否则你应该使用.join(“\ n”)。

我的问题是,为什么你从只有一个元素的数组开始?字符串可以拆分为数组,并以相同的方式连接回字符串。此外,它可能更容易,而不是使用波浪号〜分隔你想要拆分的语句,只需使用一种正确的数组语法形式,然后摆脱“拆分”,只需使用加入:

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用户来说是有问题的。