javascript - 为什么 <br> 换行而\n 在 JS 中失败

标签 javascript html output newline

我正在编写一个简单的 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>

输出:

enter image description here

我的 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>

输出:

enter image description here

最佳答案

document.write()写一个 HTML block 。在 HTML 中,<br>创建一个新行。因此,如果要在使用 document.write() 时换行, 你将不得不使用 <BR>

关于javascript - 为什么 <br> 换行而\n 在 JS 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54272879/

相关文章:

git - 了解 git 日志图输出

javascript - 使用正则表达式验证自定义表达式

javascript - 使用相同的 URL、JavaScript 打开多个迷你窗口

Javascript Express mongodb服务器无法从发布请求中获取数据

javascript - 旋转椭圆形 SVG 对象

string - 如何在 Bash 命令替换结束时禁止或删除换行符?

javascript - Highstock 具有多个系列,即使我禁用,工具提示也会跟随点

html - FORM 标记在 IE 中创建不需要的换行符

javascript - HTML onchange 不会从 Ajax 插入中触发

python - 在python中拆分一个变量表