正如您在我的 index.html 文件中看到的那样,我正在尝试创建新行(在这一行文本中),但它不起作用,有什么帮助吗? (\n 是新行应该开始的地方,
也没有用。
index.html:
<html>
<head>
<title>test</title>
</head>
<body bgcolor="black">
<font color="green">
<p id="terminal"></p>
<script>
var text = "this is a test\nthis should be on the next line";
var count = 0;
var speed = 50;
function Type() {
if(count < text.length) {
document.getElementById("terminal").innerHTML += text.charAt(count);
count ++;
setTimeout(Type, speed);
}
}
Type();
</script>
最佳答案
如果你不想使用<br />
您可以轻松使用 <pre>
标签。 <pre>
其实更容易使用因为您不必插入 <br />
在 DOM 中的正确位置。
取自docs .
The HTML pre element represents preformatted text which is to be presented exactly as written in the HTML file.
var text = "this is a test\nthis should be on the next line";
var count = 0;
var speed = 50;
function Type() {
if(count < text.length) {
document.getElementById("terminal").innerHTML += text.charAt(count);
count ++;
setTimeout(Type, speed);
}
}
Type();
<html>
<head>
<title>test</title>
</head>
<body bgcolor="black">
<font color="green" />
<pre id="terminal"></pre>
</body>
</html>
关于javascript - 如何在单个字符串中在javascript中创建一个新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49637842/