我试图在输出框中写出每个数组/函数后添加换行符,但似乎无法找到它的解决方案。我尝试将其添加为变量,评论等。任何想法?谢谢。 例如:
1,2,3,4,5,6,7,8,9,11,22,23,25 "new line"
1,2,3,4,5,6,7,8,9,11,22,23,25 "new line"
码
<!DOCTYPE HTML>
<head>
<title>Oppgave 1</title>
<meta charset="utf-8" />
<style>
</style>
<script>
window.onload = oppstart;
var output = "";
var arr = [1,2,3,4,5,6,7,8,9,11,22,23,25];
var sum = 0;
function oppstart(){
//Forlengs
for (var i = 0; i < arr.length; i++) {
skrivUt();
}
//Baklengs
for(var i = arr.length-1; i >=0; i--) {
skrivUt();
}
//Annethver
for(var i = 0; i<arr.length; i+=2) {
skrivUt();
}
//Mindre enn 10
for(var i = 0; i < arr.length; i++)
if(arr[i] < 10)
skrivUt();
//Partall
for(var i = 0; i < arr.length; i++)
if(arr[i] % 2 === 0)
skrivUt();
function skrivUt(){
output += arr[i] + " ";
//sum += arr[i];
document.getElementById("out").innerHTML = output;
}
}
</script>
</head>
<body>
<p id="out"></p>
</body>
答案 0 :(得分:0)
这不是确切的答案这只是你的问题的例子只是使用&#34; \ n&#34;对于你脚本中的下一行
window.onload = oppstart;
var output = "";
var arr = [1,2,3,4,5,6,7,8,9,11,22,23,25];
var sum = 0;
function oppstart(){
//Forlengs
for (var i = 0; i < arr.length; i++) {
skrivUt();
}
//Baklengs
for(var i = arr.length-1; i >=0; i--) {
skrivUt();
}
//Annethver
for(var i = 0; i<arr.length; i+=2) {
skrivUt();
}
//Mindre enn 10
for(var i = 0; i < arr.length; i++)
if(arr[i] < 10)
skrivUt();
//Partall
for(var i = 0; i < arr.length; i++)
if(arr[i] % 2 === 0)
skrivUt();
function skrivUt(){
output += arr[i]+"\n";
alert(output);
//sum += arr[i];
document.getElementById("out").innerHTML = output;
}
}
&#13;
<p id="out"></p>
&#13;
答案 1 :(得分:-1)
使用array.push而不是汇总值。