如何在 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(?);
}
我正在打印星星金字塔,无法打印新行。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
使用
\n
作为换行符。您还可以有多个:
但是,如果这是呈现为 HTML,您将需要使用 HTML 标记作为换行符:
源中的字符串
Hello\n\nTest
将如下所示:字符串
Hello
Test
在 HTML 源代码中将如下所示:对于查看页面的人来说,HTML 字符串将呈现为换行符,
\n 只是将文本拖放到下一行源(如果它位于 HTML 页面上)。
Use the
\n
for a newline character.You can also have more than one:
However, if this is rendering to HTML, you will want to use the HTML tag for a newline:
The string
Hello\n\nTest
in your source will look like this:The string
Hello<br><br>Test
will look like this in HTML source: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 页面中,因为单独的换行符只会显示为空格)
how about:
(assuming you are in an html page, since a line feed alone will only show as a space)
使用
标签在文档中创建换行符这是一个示例小提琴
Use a
<br>
tag to create a line break in the documentHere's a sample fiddle
或者,使用 CSS
white-space: pre
写入元素并使用\n
作为换行符。Alternatively, write to an element with the CSS
white-space: pre
and use\n
for newline character.使用
"\n"
:注意,它必须用双引号括起来才能被解释为换行符。不,不是。Use
"\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')
如果您正在寻找新的更详细的粒度使用线document.writeln()
is what you are looking for ordocument.write('\n' + 'words')
if you are looking for more granularity in when the new line is used在 html 页面中:
但如果你在 JavaScript 文件中,那么这将作为新行工作:
In html page:
but if you are in JavaScript file, then this will work as new line:
对于字符串,我只需编写
"\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, typingconsole.log("First Name: Rex" + "\n" + "Last Name: Blythe");
Will type:First Name: Rex
Last Name: Blythe
要创建新行,符号为 '\n'
如果要输出到页面,则需要使用
"
而不是"
'/n'
;JavaScript 中的转义字符
To create a new line, symbol is '\n'
If you are outputting to the page, you'll want to use
"<br/>"
instead of'/n'
;Escape characters in JavaScript
\n 不起作用。使用 html 标签
\n dosen't work. Use html tags
\n -->换行符不适用于插入新行。
但如果我们使用下面的代码,那么它可以正常工作并且给出新行。
\n --> newline character is not working for inserting a new line.
But if we use below code then it works fine and it gives new line.
你也可以像这样的星星金字塔
you can also pyramid of stars like this
如果您多次执行它 (
document.write();
),它将不起作用。我建议你应该这样做:
PS我知道人们已经在上面陈述了这个答案,但没有在任何地方发现差异,所以:)
won't work if you're executing it (
document.write();
) multiple times.I'll suggest you should go for:
P.S I know people have stated this answer above but didn't find the difference anywhere so :)
尝试在 HTML pre 标记之间编写代码。
Try to write your code between the HTML pre tag.
实际上这很容易。 :D
要换行,只需使用
\n
“function”。对于 HTML 相关项目,仅使用
,就像在实际的 HTML 脚本中一样。 :)输出
但是,请注意上面的输出在某些 HTML 相关项目中不起作用。为此,您需要使用
- 就像 HTML 中一样:DIt'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. :)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如果您使用的是 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 usedocument.write("<br/>");
.你的解决方案是
your solution is
您可以使用以下链接: JavaScript 中的新行
You can use below link: New line in javascript