将换行符插入前置标记(IE、Javascript)

发布于 2024-07-06 09:23:52 字数 396 浏览 12 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

仄言 2024-07-13 09:23:52

这些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>."

谈场末日恋爱 2024-07-13 09:23:52

这在 IE 中有效吗?

document.getElementById("putItHere")
    .appendChild(document.createTextNode("first line\nsecond line"));

我用 Firefox 测试了它,它可以工作。 :-)

Does this work in IE?

document.getElementById("putItHere")
    .appendChild(document.createTextNode("first line\nsecond line"));

I tested it with Firefox and it works. :-)

ヤ经典坏疍 2024-07-13 09:23:52

解决方法可以在已接受答案中链接的页面中找到。 为了方便使用,这里是:

if (elem.tagName == "PRE" && "outerHTML" in elem)
{
    elem.outerHTML = "<PRE>" + str + "</PRE>";
}
else
{
    elem.innerHTML = str;
}

The workaround can be found in the page linked to in the accepted answer. For ease of use here it is:

if (elem.tagName == "PRE" && "outerHTML" in elem)
{
    elem.outerHTML = "<PRE>" + str + "</PRE>";
}
else
{
    elem.innerHTML = str;
}
一指流沙 2024-07-13 09:23:52


在所有浏览器中应该只输出一行。 当然也删除\n,代码应该是:

document.getElementById("putItHere").innerHTML = "first line<br/>second line";

<br/> shoud only output one line in all browsers. Of course remove the \n as well, code should be:

document.getElementById("putItHere").innerHTML = "first line<br/>second line";
倾城°AllureLove 2024-07-13 09:23:52

 标记内的内容不应被视为 HTML。

事实上,

 标记的目的是显示格式化文本。

使用innerText 属性是修改

 标记内容的正确方法。

document.getElementById("putItHere").innerText = "first line\nsecond line";

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.

document.getElementById("putItHere").innerText = "first line\nsecond line";
や三分注定 2024-07-13 09:23:52

IE9 不标准化白色空间,与它的前身不同。

您应该测试支持而不是针对任何特定浏览器。 例如..

var t = document.createElement(elem.tagName);
t.innerHTML = "\n";

if( t.innerHTML === "\n" ){
    elem.innerHTML = str;
}
else if("outerHTML" in elem)
{
    elem.outerHTML = "<"+elem.tagName+">" + str + "</"+elem.tagName+">";
}
else {
    // fallback of your choice, probably do the first one.
}

IE9 does not normalize white spaces, unlike its predecessors.

You should test for support rather than targeting any specific browser. E.g...

var t = document.createElement(elem.tagName);
t.innerHTML = "\n";

if( t.innerHTML === "\n" ){
    elem.innerHTML = str;
}
else if("outerHTML" in elem)
{
    elem.outerHTML = "<"+elem.tagName+">" + str + "</"+elem.tagName+">";
}
else {
    // fallback of your choice, probably do the first one.
}
神爱温柔 2024-07-13 09:23:52

我估计这个

我发现 IE 正在使用 \r\n 而 Fx(其他)正在使用 \n

var newline;
if ( document.all ) newline = '\r\n';
else newline = '\n';

var data = 'firstline' + newline + 'second line';
document.getElementById("putItHere").appendChild(document.createTextNode(data));

对于我曾经制作的 TinyMCE(wysiwyg 编辑器)插件,我最终使用 BR i 编辑模式
并在提交等时清理它。

此代码循环遍历 PRE 元素内的所有 BR 元素,并用换行符替换 BR。

请注意,该代码依赖于 TinyMCE API,但可以使用标准 Javascript 轻松编写。

清理:

        var br = ed.dom.select('pre br');
        for (var i = 0; i < br.length; i++) {
          var nlChar;
          if (tinymce.isIE)
            nlChar = '\r\n';
          else
            nlChar = '\n';

          var nl = ed.getDoc().createTextNode(nlChar);
          ed.dom.insertAfter(nl, br[i]);
          ed.dom.remove(br[i]);
        }

祝你好运!

I reckon this.

What I found was IE is using \r\n and Fx(others) is using \n

var newline;
if ( document.all ) newline = '\r\n';
else newline = '\n';

var data = 'firstline' + newline + 'second line';
document.getElementById("putItHere").appendChild(document.createTextNode(data));

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:

        var br = ed.dom.select('pre br');
        for (var i = 0; i < br.length; i++) {
          var nlChar;
          if (tinymce.isIE)
            nlChar = '\r\n';
          else
            nlChar = '\n';

          var nl = ed.getDoc().createTextNode(nlChar);
          ed.dom.insertAfter(nl, br[i]);
          ed.dom.remove(br[i]);
        }

Good luck!

濫情▎り 2024-07-13 09:23:52

如果您不想使用outerHTML,如果附加的 pre 标记不是问题,您还可以对 IE 执行以下操作:

 if(isIE)
     document.getElementById("putItHere").innerHTML = "<pre>" + content+"</pre>";
 else
     document.getElementById("putItHere").innerHTML = content;

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:

 if(isIE)
     document.getElementById("putItHere").innerHTML = "<pre>" + content+"</pre>";
 else
     document.getElementById("putItHere").innerHTML = content;
入怼 2024-07-13 09:23:52

我发现 innerHTML 在应用于元素之前已被处理,因此
成为换行符并删除多个空格。

要保留原始文本,您必须使用 nodeValue,例如;

document.getElementById('pre_id').firstChild.nodeValue='    white space \r\n ad new line';

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;

document.getElementById('pre_id').firstChild.nodeValue='    white space \r\n ad new line';
ζ澈沫 2024-07-13 09:23:52

这是对爱德华·王尔德的答案的一个非常小的调整,保留了

 上的属性。  标签。

if (elem.tagName == "PRE" && "outerHTML" in elem) {
    var outer = elem.outerHTML;
    elem.outerHTML = outer.substring(0, outer.indexOf('>') + 1) + str + "</PRE>";
}
else {
    elem.innerHTML = str;
}

Here is a very small tweak to Edward Wilde's answer that preserves the attributes on the <pre> tag.

if (elem.tagName == "PRE" && "outerHTML" in elem) {
    var outer = elem.outerHTML;
    elem.outerHTML = outer.substring(0, outer.indexOf('>') + 1) + str + "</PRE>";
}
else {
    elem.innerHTML = str;
}
小…楫夜泊 2024-07-13 09:23:52
if (typeof div2.innerText == 'undefined')
    div2.innerHTML = value;
else
    div2.innerText = value;

这对我有用。

if (typeof div2.innerText == 'undefined')
    div2.innerHTML = value;
else
    div2.innerText = value;

that worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文