最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Line breaks won't display in textarea in IE - Stack Overflow

programmeradmin4浏览0评论

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.

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

Any ideas?

My code:

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

The data.php simply queries a MySql database and prints the results.

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.

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

Any ideas?

My code:

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

The data.php simply queries a MySql database and prints the results.

Share Improve this question edited May 5, 2011 at 14:25 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked May 5, 2011 at 14:17 Gary RyanGary Ryan 7543 gold badges8 silver badges20 bronze badges 1
  • 4 What constitutes the line breaks? Are they \r\n? – Matt Greer Commented May 5, 2011 at 14:19
Add a ment  | 

3 Answers 3

Reset to default 8

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:

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 !@#$ing Notepad and show up with the line breaks intact.

They're patibility 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:

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

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

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

And the following code in keydown event:

if (event.keyCode == 13) {
        event.stopPropagation();
    }
发布评论

评论列表(0)

  1. 暂无评论