Javascript NewLine 转义字符不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10225878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:23:50  来源:igfitidea点击:

NewLine escape character not working

javascripthtmlnewline

提问by pop stack

We know that \n is used to feed a new line in JavaScript.
How should I use it for an output (in a for-loop):

我们知道 \n 用于在 JavaScript 中输入一个新行。
我应该如何将它用于输出(在 for 循环中):

str=prompt("Enter any string!");
    for(i=0;i<str.length;i++)
    {
        document.write('\n'+str.charCodeAt(i));
    }   

or

或者

str=prompt("Enter any string!");
    for(i=0;i<str.length;i++)
    {
        document.write('\n'+str.charCodeAt(i));
    }

Neither seems to work.

两者似乎都不起作用。

回答by T.J. Crowder

This has nothing to do with JavaScript. In HTML, all whitespace (including newlines) is collapsed and treated as a single space.

这与 JavaScript 无关。在 HTML 中,所有空格(包括换行符)都被折叠并被视为一个空格。

To do a line break in HTML:

在 HTML 中进行换行:

  • Use <br>
  • Or organize your text into paragraphs with <p>...</p>, etc.)
  • Or if you're outputting some form of formatted text (like code), you can do that in a <pre>...</pre>element (or any element with the white-space: pre, white-space: pre-wrap, or white-space: pre-linestyle applied to it).
  • <br>
  • 或使用<p>...</p>等将您的文本组织成段落)
  • 或者,如果您输出某种形式的格式文本(比如代码),你可以做,在一个<pre>...</pre>元素(或与任何元素white-space: prewhite-space: pre-wrapwhite-space: pre-line风格适用于它)。

回答by Mikey

If you're writing to the document you'll want document.write('<br/>'+str.charCodeAt(i));- or to set your output in a <pre>tag (or another element with the a style attribute of white-space:pre).

如果您正在写入您想要的文档document.write('<br/>'+str.charCodeAt(i));- 或者将您的输出设置在<pre>标签(或具有 样式属性的另一个元素white-space:pre)中。

回答by srini

use document.writeln() method .

使用 document.writeln() 方法。

The writeln() method is identical to the write() method, with the addition of writing a newline character after each statement.

writeln() 方法与 write() 方法相同,只是在每个语句后添加了一个换行符。

try this

尝试这个

 str=prompt("Enter any string!");
 for(i=0;i<str.length;i++)
 {
    document.writeln(str.charCodeAt(i));
 } 

回答by Bugs Bunny

I made a much better solution. See it in action https://repl.it/@mamamia5x/Example

我做了一个更好的解决方案。看到它在行动https://repl.it/@mamamia5x/Example

In css do

在 css 中做

h1 span::before { 
  content: "\A";
  white-space: pre;
}

Replace the h1 with whatever you have. Now, whenever you do <span>, a new line will break. So whenever someone says /n, it'll do the <span>and make a new line.

用你拥有的任何东西替换 h1。现在,无论何时执行<span>,都会断开新的一行。因此,每当有人说 /n 时,它都会执行该操作<span>并换行。

if (txt.charAt(i) == '/' && txt.charAt(i + 1) =='n'){
   document.getElementById("text").innerHTML += " <span>";
   i = i + 2;
} 

Here is it in action https://repl.it/@mamamia5x/Example, and here is a project I am using it for https://live-stream.mamamia5x.repl.co/.

这是它在行动https://repl.it/@mamamia5x/Example,这是我将它用于https://live-stream.mamamia5x.repl.co/ 的项目

I also made it work with <br>. If you want to do that, you can do

我也让它与<br>. 如果你想这样做,你可以这样做

if (txt.charAt(i) == '<' && txt.charAt(i + 1) == 'b' && txt.charAt(i + 2) == 'r' && txt.charAt(i + 3) == '>'){

You can also mix the two together, and it can allow /nand <br>.

您也可以将两者混合在一起,它可以允许/n<br>