为数组值添加换行符
adding line breaks to array values 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的新手。我不反对自学,但我在网上找遍了,却找不到我的答案。
要制作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用户来说似乎是有问题的。
- 如何在映射数组中添加换行符
- JQuery:向多个匹配结果添加换行符的最简单方法
- javascript/jQuery:在title中添加换行符——在IE9中有效,但在IE8中无效
- 在JavaScript中的无操作(空)函数中不添加换行符的选项(IntelliJ Web/PHP风暴)
- 将换行符添加到.text内容文本
- 在javascript数组元素之间添加换行符
- D3 垂直条形图为标签文本添加换行符
- 如何在指定字符后添加换行符
- 为图像上绝对定位的文本块添加换行符以创建文本条带
- 从 PHP 向 JavaScript 添加换行符
- 我将如何使用jquery在html文本区域中按回车符时添加换行符
- 添加换行符 JavaScript pdf 表单时的文本字段空白
- Javascript在mailto正文中添加换行符
- 尝试向字符串中的每个找到的模式添加换行符
- 使用 Javascript/jquery 在
标记后添加换行符
- 向D3图形y轴标签添加换行符
- 如何使用Javascript或Ruby在大块文本中智能地添加换行符
- Javascript添加换行符, 不起作用
- 如何使用XTemplate在x次迭代后添加换行符
- 如何将换行符添加到CakePHP脚本块