如何在Javascript中创建一个新行? [英] How do I create a new line in Javascript?

查看:109
本文介绍了如何在Javascript中创建一个新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var i; (i = 10; i> = 0; i = i-1){
var s;
for(s = 0; s< s; s = s + 1){
document.write(*);
}
//我想要打印一个新行
/document.write(?);

}

我印制了一个星星金字塔,我不能打印新行。使用 \\\
换行符。

  document.write(\\\
);

您也可以有多个:

 文件撰写( \\\
\\\
\\\
); // 3个新行!我的哦,我的!

但是,如果这是渲染为HTML,则您需要将HTML标记用于换行符:

  document.write(< br>); 

字符串 Hello \\\
\\\
Test
在你的源代码中将如下所示:

 您好! 

测试

字串您好< br> < br> Test< / code>在HTML源代码中看起来像这样:

  Hello< br>< br>测试

HTML查看页面的人将呈现换行符, \\\
只是将文本放到源代码中的下一行(如果它在HTML页面上)。


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.

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

这篇关于如何在Javascript中创建一个新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆