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
作为换行符。
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<br><br>Test
在HTML源代码中将如下所示:
Hello<br><br>Test
HTML一个将呈现为查看页面的人的换行符,\n
只是将文本放到源中的下一行(如果它在HTML页面上)。
\ n - >换行符不适用于插入新行。
str="Hello!!";
document.write(str);
document.write("\n");
document.write(str);
但是,如果我们使用下面的代码,那么它工作正常,它提供了新的线。
document.write(str);
document.write("<br>");
document.write(str);
注意::我在Visual Studio Code中尝试过。
你的解决方案是
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>");
}
\ n不要工作。使用html标签
document.write("<br>");
document.write("?");
如果您使用的是JavaScript文件(.js),请使用document.write("\n");
。如果您使用的是html文件(.html或.htm),请使用document.write("<br/>");
。
document.write("\n");
如果您多次执行它(document.write();
)将无法工作。
我建议你应该去:
document.write("<br>");
P.S我知道人们已经在上面说过这个答案,但没有发现任何地方的差异所以:)
用于添加新行使用
document.write("<br>")
用于增加空间使用
document.write(" ")
要么
document.write("\n")
尝试在HTML pre标记之间编写代码。
您可以使用以下链接: 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>');
}
怎么样:
document.write ("<br>");
(假设您在html页面中,因为单独的换行仅显示为空格)
使用"\n"
:
document.write("\n");
注意,它必须用双引号括起来,以便将其解释为换行符。 不,不。
document.writeln()
是您正在寻找的或document.write('\n' + 'words')
如果您在使用新线时寻找更多粒度
要创建新行,符号为'\ 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');
}
如果您要输出到页面,则需要使用"<br/>"
而不是'/n'
;
在html页面中:
document.write("<br>");
但是如果你在JavaScript文件中,那么这将作为新行:
document.write("\n");
或者,使用CSS white-space: pre
写入元素,并使用\n
作为换行符。
对于一个字符串我只是写"\n"
给我一个新的线。例如,输入console.log("First Name: Rex" + "\n" + "Last Name: Blythe");
将输入:
名字:雷克斯
姓氏:布莱斯