javascript 如何在javascript中打印新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31334188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to print new line in javascript
提问by Poushali
<!DOCTYPE html>
<html>
<head>
<title>multiplication table</title>
</head>
<body>
<script type="text/javascript">
var i, j;
var m = 1;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
document.write(i * j);
}
document.write("\n");
}
</script>
</body>
</html>
Output:
输出:
12345678910 2468101214161820 36912151821242730 481216202428323640 5101520253035404550 6121824303642485460 7142128354249566370 8162432404856647280 9182736455463728190 102030405060708090100
12345678910 2468101214161820 36912151821242730 481216202428323640 5101520253035404550 6121824303642485460 7142128354249566370 8162432404856647280 9182736455463728190 102030405060708090100
it gives output without printing the new line. I want to execute this without using
它提供输出而不打印新行。我想在不使用的情况下执行此操作
回答by Tushar
Use <br />
to print text on new line. When you are putting the content in DOM
. \n
will just add a space instead of newline.
用于<br />
在新行上打印文本。当您将内容放入DOM
. \n
只会添加一个空格而不是换行符。
document.write("<br />");
Example:
例子:
var i, j;
var m = 1;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
document.write(i * j);
}
document.write("<br />");
}
回答by Riya Agrahari
Instead of document.write("\n");in your code add document.write("
而不是document.write("\n"); 在您的代码中添加 document.write("
Code Goes here -
代码在这里 -
<!DOCTYPE html>
<html>
<head>
<title>multiplication table</title>
</head>
<body>
<script type="text/javascript">
var i, j;
var m = 1;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
document.write(i * j);
}
document.write("<br/>");
}
</script>
</body>
</html>
回答by Sím?n Da?g?l
var i, j;
var m = 1;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
document.write(i * j + "\n\n");
}
document.write("<br />");
}
回答by Nikhil Batra
Replace document.write("\n");
with document.write("<br />");
替换document.write("\n");
为document.write("<br />");
See the fiddle: "https://jsfiddle.net/godcb7L2/"
见小提琴:“ https://jsfiddle.net/godcb7L2/”
回答by Vikas
<html>
<body>
<script type="text/javascript">
var i, j;
var m = 1;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
document.write(i * j);
}
document.write("<br />");
}
</script>
</body>
</html>
回答by Simpal Kumar
Did you try <br/>
, <br><br/>
? <br>
should be supported according to this source, though.
你有没有尝试<br/>
,<br><br/>
?<br>
不过,应该根据这个来源得到支持。
Like: document.write("<br/>")
喜欢: document.write("<br/>")