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

javascript - Textarea in IE8 Newline issue - Stack Overflow

programmeradmin4浏览0评论

I'm making a cross-browser form, which requires a textarea. This textarea receives data by two ways: - Sending an Ajax call to the server and returning data - User inputted data

/

I'm having an issue on IE8 where the textarea text is not formatted (newlines not being read), where as in Chrome, the same code, nicely formats the textarea. I'm hoping to use a solely JavaScript solution if possible, but I'm not sure where to go from here.

I'm making a cross-browser form, which requires a textarea. This textarea receives data by two ways: - Sending an Ajax call to the server and returning data - User inputted data

http://jsfiddle/garrettwong/x3KSP/

I'm having an issue on IE8 where the textarea text is not formatted (newlines not being read), where as in Chrome, the same code, nicely formats the textarea. I'm hoping to use a solely JavaScript solution if possible, but I'm not sure where to go from here.

Share Improve this question edited Jun 5, 2012 at 0:02 Fabrício Matté 70.2k27 gold badges133 silver badges167 bronze badges asked Jun 4, 2012 at 23:56 GarrettGarrett 1,6882 gold badges17 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Why are you using text()? Set the value using val().

New lines are normally \n\r.

Do a replace operation to ensure that there is a \r before every \n:

str = str.replace(/\r?\n/g, "\r\n");

Fiddle

Browsers use \r\n for line breaks, though modern browsers such as Chrome and Firefox can also handle Unix-style \n line breaks. IE8 requires proper \r\n line breaks, which work in all other browsers as well.

And as @epascarello noted, use .val() to manipulate the textarea's value property.

Just use "\r\n" for new lines.

发布评论

评论列表(0)

  1. 暂无评论