我正在编写一个简单的 javascript 程序,我发现当我在 document.write("");
中使用 \n
时,它不会一条新线。当我使用 <br
> 标签时,它起作用了。
我想知道为什么\n
无法换行而<br
> 换行。
注意:我是 Javascript 的新手...
我的 Javascript 使用 br:
<head>
<style>
</style>
</head>
<body>
<h1>Foo App</h1>
<script charset="utf-8">
var input = null;
input = prompt("Enter something: ");
for (var i = 0; i < 10; i++) {
document.write("Your input: " + input + "<br><br>");
}
</script>
</body>
输出:
我的 javascript 使用 \n
:
<body>
<h1>Foo App</h1>
<script charset="utf-8">
var input = null;
input = prompt("Enter something: ");
for (var i = 0; i < 10; i++) {
document.write("Your input: " + input + "\n\n");
}
</script>
</body>
输出:
最佳答案
document.write()
写一个 HTML block 。在 HTML 中,<br>
创建一个新行。因此,如果要在使用 document.write()
时换行, 你将不得不使用 <BR>
关于javascript - 为什么 <br> 换行而\n 在 JS 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54272879/