gpt4 book ai didi

javascript - 使用 "\n"字符在javascript中创建换行符

转载 作者:行者123 更新时间:2023-12-02 22:01:04 26 4
gpt4 key购买 nike

我正在用 javascript 编写一个非常基本的代码,我期待使用 "\n"对于换行符,但它似乎不起作用。

我在其他帖子中看到了替代解决方案,但我想知道为什么是 "\n"不在这里工作。

这是我的代码。

<html>

<body>
<h2>welcome to java script</h2>
<script>
var x = 10;
var y = 20;
var sum = x + y;
var mult = x * y;
document.write("the sum is " + sum);
document.write("\n");
document.write(" the product is " + mult);
</script>
</body>

</html>

最佳答案

换行符 ( \n ) 在不在文档中的字符串中换行。

要在文档中换行,您必须使用 HTML <br/>元素。

<html>

<body>
<h2>welcome to java script</h2>
<script>
var x = 10;
var y = 20;
var sum = x + y;
var mult = x * y;
document.write("the sum is " + sum);
document.write("<br/>");
document.write(" the product is " + mult);
</script>
</body>

</html>

关于javascript - 使用 "\n"字符在javascript中创建换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59871752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com