JQuery .html() remove line break on IE 8

2019-07-15 10:10发布

问题:

So I've been trying to decode string with .html() function in jQuery and it work really nice except on IE...

Here is the string I have:

ééé\r\nààà

I want this to be:

ééé\r\nààà

and I currently get after .html() with IE:

ééé ààà

So this seems nice on FF and Chrome but on IE all linebreak are removed. I found an article (http://web.student.tuwien.ac.at/~e0226430/innerHtmlQuirk.html) explaining the problem is with .innerHTML used by .html() function...

I'm actually surprised to not found a topic about that. Is there any solution? Maybe do a specific function to decode that on IE?

For more here is the code:

var itemDescription = "ééé\r\nàà&#224";
$('.feeds').find('textarea.description[ifid="' + ifid + '"]').html(itemDescription);

回答1:

Try This:

 var itemDescription = "ééé\\r\\nàà&#224";


回答2:

Actually one solution I found is to do something like that:

itemDescription = itemDescription.replace(/(\r\n|\r|\n)/g, '________BREAK________');
var decodedDescription = $("<div>").html(itemDescription).text();
decodedDescription = decodedDescription.replace(/________BREAK________/g, '\r\n');