NewLine转义字符不起作用 [英] NewLine escape character not working

查看:271
本文介绍了NewLine转义字符不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道\\\
用于在JavaScript中提供新行。

我应该如何将它用于输出(在for-loop中):

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

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

两者似乎都没有用。

解决方案

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



在HTML中执行换行符:


  • 使用< br>
  • /www.w3.org/TR/html5/the-p-element.html#the-p-elementrel =noreferrer> < p> ...< / p> 等)
  • 或者如果您要输出某种格式的文本(如代码),您可以在 < pre> ...< ; / pre> 元素(或者 white-space:pre 空格:pre-wrap ,或空格:适用于该行的前行样式)。


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

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.

解决方案

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

To do a line break in 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-line style applied to it).

这篇关于NewLine转义字符不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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