如何在 Javascript 中创建一个新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5758161/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:44:06  来源:igfitidea点击:

How do I create a new line in Javascript?

javascripthtmlnewlineline-breaks

提问by Technupe

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   //i want this to print a new line
   /document.write(?);

}

I am printing a pyramid of stars, I can't get the new line to print.

我正在打印星星的金字塔,我无法打印新的线条。

回答by Tom Gullen

Use the \nfor a newline character.

\n用于换行符。

document.write("\n");

You can also have more than one:

您也可以拥有多个:

document.write("\n\n\n"); // 3 new lines!  My oh my!

However, if this is rendering to HTML, you will want to use the HTML tag for a newline:

但是,如果这是呈现为 HTML,您将需要使用 HTML 标记作为换行符:

document.write("<br>");

The string Hello\n\nTestin your source will look like this:

Hello\n\nTest源中的字符串将如下所示:

Hello!

Test

The string Hello<br><br>Testwill look like this in HTML source:

该字符串Hello<br><br>Test在 HTML 源代码中将如下所示:

Hello<br><br>Test

The HTML one will render as line breaks for the person viewing the page, the \njust drops the text to the next line in the source (if it's on an HTML page).

HTML 将呈现为查看页面的人的换行符,\n只是将文本放到源中的下一行(如果它在 HTML 页面上)。

回答by rob

how about:

怎么样:

document.write ("<br>");

(assuming you are in an html page, since a line feed alone will only show as a space)

(假设您在 html 页面中,因为单独的换行只会显示为空格)

回答by JaredPar

Use a <br>tag to create a line break in the document

使用<br>标签在文档中创建换行符

document.write("<br>");

Here's a sample fiddle

这是一个示例小提琴

回答by Jared Farrish

Use "\n":

使用"\n"

document.write("\n");

Note, it has to be surrounded in double quotes for it to be interpreted as a newline.No it doesn't.

请注意,它必须用双引号括起来才能被解释为换行符。不,它没有。

回答by acconrad

document.writeln()is what you are looking for or document.write('\n' + 'words')if you are looking for more granularity in when the new line is used

document.writeln()是您正在寻找的内容,或者document.write('\n' + 'words')您是否正在寻找使用新行时的更多粒度

回答by kemiller2002

To create a new line, symbol is '\n'

要创建一个新行,符号是 '\n'

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   //i want this to print a new line
   document.write('\n');

}

If you are outputting to the page, you'll want to use "<br/>"instead of '/n';

如果要输出到页面,则需要使用"<br/>"而不是'/n';

Escape characters in JavaScript

JavaScript 中的转义字符

回答by Suhani Mendapara

In html page:

在 html 页面中:

document.write("<br>"); 

but if you are in JavaScript file, then this will work as new line:

但是如果你在 JavaScript 文件中,那么这将作为新行工作:

document.write("\n");

回答by Shadab Ali

you can also pyramid of stars like this

你也可以像这样的星星金字塔

for (var i = 5; i >= 1; i--) {
     var py = "";
     for (var j = i; j >= 1; j--) {
         py += j;

     }
     console.log(py);
 }

回答by alex

Alternatively, write to an element with the CSS white-space: preand use \nfor newline character.

或者,white-space: pre使用CSS 写入元素并\n用于换行符。

回答by Tito E Surek

For a string I just write "\n"to give me a new line. For example, typing console.log("First Name: Rex" + "\n" + "Last Name: Blythe");Will type:

对于一个字符串,我只是写给"\n"我一个新行。例如,键入console.log("First Name: Rex" + "\n" + "Last Name: Blythe");Will 键入:

First Name: Rex

名字:雷克斯

Last Name: Blythe

姓氏:布莱斯