如何在 Javascript 中创建新行?

发布于 2024-11-03 11:04:58 字数 219 浏览 2 评论 0原文

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(?);

}

我正在打印星星金字塔,无法打印新行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

山色无中 2024-11-10 11:04:58

使用 \n 作为换行符。

document.write("\n");

您还可以有多个:

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

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

document.write("<br>");

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

Hello!

Test

字符串 Hello

Test 在 HTML 源代码中将如下所示:

Hello<br><br>Test

对于查看页面的人来说,HTML 字符串将呈现为换行符,\n 只是将文本拖放到下一行源(如果它位于 HTML 页面上)。

Use the \n for a newline character.

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:

document.write("<br>");

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

Hello!

Test

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

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).

如果没有 2024-11-10 11:04:58

怎么样:(

document.write ("<br>");

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

how about:

document.write ("<br>");

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

海未深 2024-11-10 11:04:58

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

document.write("<br>");

这是一个示例小提琴

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

document.write("<br>");

Here's a sample fiddle

月依秋水 2024-11-10 11:04:58

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

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

依 靠 2024-11-10 11:04:58

使用 "\n":

document.write("\n");

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

Use "\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.

雨轻弹 2024-11-10 11:04:58

document.writeln() 是您正在寻找的,或者 document.write('\n' + 'words') 如果您正在寻找新的更详细的粒度使用线

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

只涨不跌 2024-11-10 11:04:58

在 html 页面中:

document.write("<br>"); 

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

document.write("\n");

In html page:

document.write("<br>"); 

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

document.write("\n");
帅气尐潴 2024-11-10 11:04:58

对于字符串,我只需编写 "\n" 即可换行。例如,输入 console.log("First Name: Rex" + "\n" + "Last Name: Blythe"); 将键入:

First Name: Rex

Last Name: Blythe

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:

First Name: Rex

Last Name: Blythe

迷爱 2024-11-10 11:04:58

要创建新行,符号为 '\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');

}

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

JavaScript 中的转义字符

To create a new line, symbol is '\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';

Escape characters in JavaScript

白鸥掠海 2024-11-10 11:04:58

\n 不起作用。使用 html 标签

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

\n dosen't work. Use html tags

document.write("<br>");
document.write("?");
友谊不毕业 2024-11-10 11:04:58

\n -->换行符不适用于插入新行。

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

但如果我们使用下面的代码,那么它可以正常工作并且给出新行。

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

注意::我在Visual Studio Code中尝试过。

\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.

同展鸳鸯锦 2024-11-10 11:04:58

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

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

     }
     console.log(py);
 }

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);
 }
叫嚣ゝ 2024-11-10 11:04:58
document.write("\n");

如果您多次执行它 (document.write();),它将不起作用。

我建议你应该这样做:

document.write("<br>");

PS我知道人们已经在上面陈述了这个答案,但没有在任何地方发现差异,所以:)

document.write("\n");

won't work if you're executing it (document.write();) multiple times.

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 :)

究竟谁懂我的在乎 2024-11-10 11:04:58

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

Try to write your code between the HTML pre tag.

黯然#的苍凉 2024-11-10 11:04:58

实际上这很容易。 :D

要换行,只需使用 \n “function”。
对于 HTML 相关项目,仅使用
,就像在实际的 HTML 脚本中一样。 :)

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   // Here's the change
   document.write('\n');

}

输出

*
*
*
*
*
*
*
*
*
*
*

但是,请注意上面的输出在某些 HTML 相关项目中不起作用。为此,您需要使用
- 就像 HTML 中一样:D

It's pretty easy actually. :D

To make a new line, you only need to use \n "function".
For HTML related-projects, use only <br>, like in actual HTML script. :)

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   // Here's the change
   document.write('\n');

}

THE OUTPUT

*
*
*
*
*
*
*
*
*
*
*

BUT, be careful the output above won't work in some HTML related projects. For that, you need to use <br> - like in HTML :D

逆夏时光 2024-11-10 11:04:58

如果您使用的是 JavaScript 文件 (.js),请使用 document.write("\n");。如果您位于 html 文件(.html 或 .htm)中,请使用 document.write("
");

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

别低头,皇冠会掉 2024-11-10 11:04:58

你的解决方案是

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

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>");
}
明天过后 2024-11-10 11:04:58

您可以使用以下链接: 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>');

}

You can use below link: 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('<br>');

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文