如何在Javascript中创建新行?

时间:2022-02-28 01:28:15
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.

我正在印刷一个金字塔的星星,我无法得到新的线条打印。

15 个解决方案

#1


141  

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

测试在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 1将作为查看页面的人的换行符,\n将文本放到源文件的下一行(如果它在HTML页面上)。

#2


22  

how about:

如何:

document.write ("<br>");

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

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

#3


9  

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

使用
标记在文档中创建换行符

document.write("<br>");

Here's a sample fiddle

下面是一个示例小提琴

#4


5  

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.

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

#5


3  

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

writeln()是您要查找的或文档。如果您希望在使用新行时获得更多的粒度,请编写('\n' + 'words')

#6


3  

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

#7


2  

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';

如果你要输出到页面,你需要使用“
”而不是“/n”;

Escape characters in JavaScript

转义字符在JavaScript中

#8


2  

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”来给我一个新的行。例如,键入控制台。日志(“名:Rex”+“\n”+“最后名:Blythe”);将类型:

First Name: Rex

第一个名字:雷克斯

Last Name: Blythe

姓名:布莱斯

#9


1  

Alternatively, write to an element with the CSS white-space: pre and use \n for newline character.

另一种方法是,使用CSS空格:pre和use \n对换行字符进行写入。

#10


1  

\n dosen't work. Use html tags

\ n不工作。使用html标记

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

#11


1  

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

P.S I know people have stated this answer above but didn't find the difference anywhere so :)

P。我知道人们已经说过以上的答案,但没有发现任何不同之处。

#12


1  

For adding a new line use

用于添加新的行使用

document.write("<br>")

For adding space use

用于添加空间

document.write("&nbsp;")

or

document.write("\n")

#13


1  

\n --> newline character is not working for inserting a new line.

>换行字符不能用于插入新行。

    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代码。

#14


0  

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

#15


-1  

If you are using a JavaScript file (.js) then use document.write("\n");. If you are in a html file (.html or . htm) then use document.write("<br/>");.

如果您正在使用一个JavaScript文件(.js),那么使用document.write("\n");如果您在一个html文件(。html或。htm)然后使用document . write(“< br / >”);。

#1


141  

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

测试在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 1将作为查看页面的人的换行符,\n将文本放到源文件的下一行(如果它在HTML页面上)。

#2


22  

how about:

如何:

document.write ("<br>");

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

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

#3


9  

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

使用
标记在文档中创建换行符

document.write("<br>");

Here's a sample fiddle

下面是一个示例小提琴

#4


5  

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.

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

#5


3  

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

writeln()是您要查找的或文档。如果您希望在使用新行时获得更多的粒度,请编写('\n' + 'words')

#6


3  

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

#7


2  

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';

如果你要输出到页面,你需要使用“
”而不是“/n”;

Escape characters in JavaScript

转义字符在JavaScript中

#8


2  

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”来给我一个新的行。例如,键入控制台。日志(“名:Rex”+“\n”+“最后名:Blythe”);将类型:

First Name: Rex

第一个名字:雷克斯

Last Name: Blythe

姓名:布莱斯

#9


1  

Alternatively, write to an element with the CSS white-space: pre and use \n for newline character.

另一种方法是,使用CSS空格:pre和use \n对换行字符进行写入。

#10


1  

\n dosen't work. Use html tags

\ n不工作。使用html标记

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

#11


1  

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

P.S I know people have stated this answer above but didn't find the difference anywhere so :)

P。我知道人们已经说过以上的答案,但没有发现任何不同之处。

#12


1  

For adding a new line use

用于添加新的行使用

document.write("<br>")

For adding space use

用于添加空间

document.write("&nbsp;")

or

document.write("\n")

#13


1  

\n --> newline character is not working for inserting a new line.

>换行字符不能用于插入新行。

    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代码。

#14


0  

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

#15


-1  

If you are using a JavaScript file (.js) then use document.write("\n");. If you are in a html file (.html or . htm) then use document.write("<br/>");.

如果您正在使用一个JavaScript文件(.js),那么使用document.write("\n");如果您在一个html文件(。html或。htm)然后使用document . write(“< br / >”);。