将换行符插入前置标记(IE、Javascript)
在 IE 中,当我将文本插入
标记时,换行符将被忽略:
<pre id="putItHere"></pre>
<script>
function putText() {
document.getElementById("putItHere").innerHTML = "first line\nsecond line";
}
</script>
使用 \r\n
而不是普通的 \n
不起作用。
确实有效,但在 FF 中插入了一个额外的空行,这对于我的目的来说是不可接受的。
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>
Using \r\n
instead of a plain \n
does not work.
<br/>
does work but inserts an extra blank line in FF, which is not acceptable for my purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这些quirksmode.org 错误报告和评论有关 Internet 的innerHTML 行为Explorer 可以提供帮助:
“IE 将 HTML 规范化应用于分配给 innerHTML 属性的数据。这会导致应保留格式的元素中的空白显示不正确,例如
These quirksmode.org bug report and comments about innerHTML behaviour of Internet Explorer could help:
"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 中有效吗?
我用 Firefox 测试了它,它可以工作。 :-)
Does this work in IE?
I tested it with Firefox and it works. :-)
解决方法可以在已接受答案中链接的页面中找到。 为了方便使用,这里是:
The workaround can be found in the page linked to in the accepted answer. For ease of use here it is:
在所有浏览器中应该只输出一行。 当然也删除\n,代码应该是:<br/>
shoud only output one line in all browsers. Of course remove the \n as well, code should be:事实上,
使用innerText 属性是修改
Content inside the
<pre>
tag should not be considered HTML.In fact, the point of
<pre>
tag is so that it does display formatted text.Using the innerText property is the correct way to modify the content of a
<pre>
tag.IE9 不标准化白色空间,与它的前身不同。
您应该测试支持而不是针对任何特定浏览器。 例如..
IE9 does not normalize white spaces, unlike its predecessors.
You should test for support rather than targeting any specific browser. E.g...
我估计这个
我发现 IE 正在使用 \r\n 而 Fx(其他)正在使用 \n
对于我曾经制作的 TinyMCE(wysiwyg 编辑器)插件,我最终使用 BR i 编辑模式
并在提交等时清理它。
此代码循环遍历 PRE 元素内的所有 BR 元素,并用换行符替换 BR。
请注意,该代码依赖于 TinyMCE API,但可以使用标准 Javascript 轻松编写。
清理:
祝你好运!
I reckon this.
What I found was IE is using \r\n and Fx(others) is using \n
For a TinyMCE(wysiwyg editor) plugin I once made I ended up with using BR i edit mode
and cleaned it up on submit etc.
This code loops through all BR elements inside PRE elements and replaces BR with newlines.
Note that the code relies on the TinyMCE API, but can easily be written using standard Javascript.
Clean up:
Good luck!
如果您不想使用outerHTML,如果附加的 pre 标记不是问题,您还可以对 IE 执行以下操作:
If you don't want to use outerHTML, you can also do the following for IE, if an additional pre tag is not an issue:
我发现 innerHTML 在应用于元素之前已被处理,因此
成为换行符并删除多个空格。
要保留原始文本,您必须使用 nodeValue,例如;
I've found that innerHTML is processed before it is applied to the element, hence <br> becomes a newline and multiple white spaces are removed.
To preserve the raw text you must use nodeValue, for example;
这是对爱德华·王尔德的答案的一个非常小的调整,保留了
Here is a very small tweak to Edward Wilde's answer that preserves the attributes on the <pre> tag.
这对我有用。
that worked for me.