简体   繁体   English

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

[英]How do I create a new line in Javascript?

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.我正在打印星星的金字塔,我无法打印新的线条。

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 页面上)。

how about:怎么样:

document.write ("<br>");

(assuming you are in an html page, since a line feed alone will only show as a space) (假设您在 html 页面中,因为单独的换行只会显示为空格)

Use a <br> tag to create a line break in the document使用<br>标签在文档中创建换行符

document.write("<br>");

Here's a sample fiddle这是一个示例小提琴

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.不,它没有。

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

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");

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 中的转义字符

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

For a string I just write "\\n" to give me a new line.对于一个字符串,我只写"\\n"给我一个新行。 For example, typing console.log("First Name: Rex" + "\\n" + "Last Name: Blythe");例如,输入console.log("First Name: Rex" + "\\n" + "Last Name: Blythe"); Will type:将键入:

First Name: Rex名字:雷克斯

Last Name: Blythe姓氏:布莱斯

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);
 }

\\n --> newline character is not working for inserting a new line. \\n --> 换行符不适用于插入新行。

    str="Hello!!";
    document.write(str);
    document.write("\n");
    document.write(str);

But if we use below code then it works fine and it gives new line.但是如果我们使用下面的代码,那么它就可以正常工作并给出新的一行。

    document.write(str);
    document.write("<br>");
    document.write(str);

Note:: I tried in Visual Studio Code .注意:我在Visual Studio Code 中尝试过。

your solution is你的解决方案是

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   //printing new line
   document.write("<br>");
}

尝试在 HTML pre 标记之间编写代码。

\\n dosen't work. \\n 没用。 Use html tags使用 html 标签

document.write("<br>");
document.write("?");

If you are using a JavaScript file (.js) then use document.write("\\n");如果您使用的是 JavaScript 文件 (.js),请使用document.write("\\n"); . . If you are in a html file (.html or . htm) then use document.write("<br/>");如果您在 html 文件(.html 或 .htm)中,则使用document.write("<br/>"); . .

document.write("\n");

won't work if you're executing it ( document.write(); ) multiple times.如果您多次执行它( document.write(); ),它将不起作用。

I'll suggest you should go for:我建议你应该去:

document.write("<br>");

PS I know people have stated this answer above but didn't find the difference anywhere so :) PS我知道人们在上面已经说明了这个答案,但在任何地方都没有发现区别所以:)

You can use below link: New line in javascript您可以使用以下链接: JavaScript 中的新行

  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('<br>');

}

if the program is integrated into html use;如果程序集成到 html 中使用; document.write("文档.写(“
"); ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM