javascript 换行符不会显示在 IE 的 textarea 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5899202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:44:35  来源:igfitidea点击:

Line breaks won't display in textarea in IE

javascriptjqueryhtmlajaxinternet-explorer

提问by Gary Ryan

Using jQuery's .load()method I'm loading text into a textarea. Works fine in Chrome & FF. As always, IE just has to be different and won't display the line breaks.

使用 jQuery 的.load()方法,我将文本加载到 textarea 中。在 Chrome 和 FF 中运行良好。与往常一样,IE 只需要有所不同并且不会显示换行符。

I've tried white-space:pre-wrapwith no luck.

我试过white-space:pre-wrap没有运气。

Any ideas?

有任何想法吗?

My code:

我的代码:

$('#textarea').load('data.php');

The data.phpsimply queries a MySql database and prints the results.

data.php简单查询MySQL数据库,并打印出结果。

采纳答案by Gary Ryan

They're compatibility problems with IE when using innerHTML(). As Jquery's .html()and .load()methods both use innerHTML(), they by extension can result in some issues. One solution is to use .text()instead. If you want to load text into a <textarea>using AJAX and Jquery you need to do something like this:

它们是 IE 使用时的兼容性问题innerHTML()。由于 Jquery.html().load()方法都使用innerHTML(),它们通过扩展可能会导致一些问题。一种解决方案是.text()改用。如果要<textarea>使用 AJAX 和 Jquery将文本加载到 a中,则需要执行以下操作:

$('#textarea').post('data.php',function(data){
        $(this).text(data);
    })
);

回答by Matt Ball

This is probably a line-ending issue. Are the line breaks in the text \ns or \r\ns? If they're just \ns try normalizing the text. You can do this with a bit of JavaScript:

这可能是行尾问题。文本中\n的换行符是s 还是\r\ns?如果他们只是\n尝试标准化文本。你可以用一些 JavaScript 来做到这一点:

function normalizeNewlines(text)
{
    return text.replace(/(\r\n|\r|\n)/g, '\r\n');
}

This is a solution that's worked for me in the past when doing essentially the reverse: I needed to take text out of a <pre>that could be pasted into !@#$ingNotepad and show up with the line breaks intact.

这是一个过去对我有用的解决方案,当我基本上做相反的事情时:我需要从<pre>可以粘贴到!@#$ing记事本中的文本中取出文本并显示完整的换行符。

回答by user2751323

I used kendoui grid in inline edit mode, for description field textare were applyed, the solution were for ie the inline style:

我在内联编辑模式下使用了kendoui网格,对于描述字段textare被应用,解决方案是针对内联样式:

<textarea style="white-space:pre-wrap;" name="' + options.field + '" ...

And the following code in keydown event:

以及 keydown 事件中的以下代码:

if (event.keyCode == 13) {
        event.stopPropagation();
    }