如何在 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
How do I create a new line in Javascript?
提问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 \n
for 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\nTest
in your source will look like this:
Hello\n\nTest
源中的字符串将如下所示:
Hello!
Test
The string Hello<br><br>Test
will 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 \n
just 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'
;
回答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: pre
and use \n
for 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
姓氏:布莱斯