这里有一个关于数组的工作代码,但是我希望我的输入在新的行中显示,而不是一行。我试过\n但没有运气。
这是我的代码:
<html>
<meta charset="UTF-8">
<head>
<title>Sample Array</title>
<script>var index = 0;</script>
</head>
<body>
Input:
<input type="text" id="input">
<input type="button" value="GO" onClick="press(input.value)">
<p id = "display">
<script>
var sample = new Array();
function press(Number)
{
if ( Number != '' )
{
sample[index] = Number;
document.getElementById("display").innerHTML += sample[index] + "\n";
index++;
}
else
alert("empty");
}
</script>
</body>
</html> 我想在document.getElementById中编码换行符。提前谢谢。
发布于 2014-01-16 18:04:21
您正在处理HTML,它将大多数空白字符视为单个空格。只需连接一个<br>
document.getElementById("display").innerHTML += sample[index] + "<br>";https://stackoverflow.com/questions/21169449
复制相似问题