[英]Preservation of tabs (indentation) on jquery html()
I'm writing a very basic site that has a textarea
and a div
. 我正在写一个非常基本的网站,有一个textarea
和一个div
。 When text is input in the text area, it gets processed by some JavaScript functions resulting in the final HTML markup 当在文本区域中输入文本时,它会被一些JavaScript函数处理,从而产生最终的HTML标记
Problem is, tabs ( \\t
) are not preserved. 问题是,标签( \\t
)不会被保留。 Is there a way to do this? 有没有办法做到这一点? Maybe replacing all \\t
by a certain HTML tag (like replacing line breaks with <br/>
)? 也许更换所有\\t
通过一定的HTML标签(如更换与换行<br/>
)?
Use white-space: pre
on the HTML ( <div>
) element. 在HTML( <div>
)元素上使用white-space: pre
。 You won't have to modify the text - it will format itself. 您不必修改文本 - 它将自行格式化。
pre 预
Sequences of whitespace are preserved, lines are only broken at newline characters in the source and at 保留了空白的序列,只在源和中的换行符处断行
elements. 元素。
For a code-only solution, replace them with multiple non-break spaces (eg
) - repeat to suit your indenting level (4 and 8 are spaces are most common). 对于仅限代码的解决方案,请将其替换为多个非中断空格(例如
) - 重复以适应您的缩进级别(4和8是最常见的空格)。
var str = "\tline 1";
str = str.replace(/\t/g, " ");
alert(str);
use a regex with /g
as replace
with a string does not support multiple matches. 使用带/g
的正则表达式replace
为字符串不支持多个匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.