本文介绍了JQuery .html()删除IE 8上的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直试图用jQuery中的.html()函数来解码字符串,除了在IE上它工作得非常好......



这里是字符串我有:

 ééé \r\\\
&# 224;&安培;#224;&安培;#224;

我希望这是:

 ééé\r\\\
ààà

我现在得到.html()与IE:

 éééààà

所以这在FF和Chrome上看起来不错,但是在IE上所有的linebreak都被删除了。
我找到了一篇文章()解释问题与.html()函数使用的.innerHTML ...

I'米实际上很惊讶没有找到关于这个话题。有没有解决方法?
也许做一个特定的函数来解码在IE浏览器?



更多这里是代码:

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


解决方案

实际上,我找到的一个解决方案就是做这样的事情:

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


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:

&#233;&#233;&#233;\r\n&#224;&#224;&#224;

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 = "&#233;&#233;&#233;\r\n&#224;&#224;&#224";
$('.feeds').find('textarea.description[ifid="' + ifid + '"]').html(itemDescription);
解决方案

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');

这篇关于JQuery .html()删除IE 8上的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 11:15