将换行符插入预标记(IE,Javascript) [英] Inserting a newline into a pre tag (IE, Javascript)
问题描述
在IE中,当我将文本插入< pre>
标记时,新行会被忽略:
In IE when I insert text into a <pre>
tag the newlines are ignored:
<pre id="putItHere"></pre>
<script>
function putText() {
document.getElementById("putItHere").innerHTML = "first line\nsecond line";
}
</script>
使用 \\\\ n
代替普通的 \ n
不起作用。
Using \r\n
instead of a plain \n
does not work.
< br />
确实有效但在FF中插入了一个额外的空行,这是不可接受的我的目的。
<br/>
does work but inserts an extra blank line in FF, which is not acceptable for my purposes.
推荐答案
这些 quirksmode.org错误报告和评论关于Internet Explorer的innerHTML行为可能有所帮助:
These quirksmode.org bug report and comments about innerHTML behaviour of Internet Explorer could help:
IE将 HTML规范化应用于分配给innerHTML属性的数据。这会导致在应该保留格式的元素中错误显示空白,例如< pre>和< textarea>。
"IE applies HTML normalization to the data that is assigned to the innerHTML property. This causes incorrect display of whitespace in elements that ought to preserve formatting, such as <pre> and <textarea>."
这篇关于将换行符插入预标记(IE,Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!