4

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.net/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.

4

3 に答える 3

8

なぜあなたは使用していtext()ますか?を使用して値を設定しますval()

改行は通常\n\r.

于 2012-06-05T00:01:00.263 に答える
5

\rbefore everyがあることを確認するために置換操作を実行し\nます。

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

フィドル

ブラウザーは\r\n改行に使用しますが、Chrome や Firefox などの最新のブラウザーは Unix スタイル\nの改行も処理できます。IE8 では適切な\r\n改行が必要ですが、これは他のすべてのブラウザーでも機能します。

@epascarello が指摘したように、textarea.val()のプロパティを操作するために使用しvalueます。

于 2012-06-05T00:03:00.653 に答える
2

改行には「\r\n」を使用してください。

于 2012-06-05T00:01:29.053 に答える