Problem with IE that removes newlines from $ ("# content"). text ()

a problem with IE that removes newlines from $ ("# content"). text ()

HTML code

<div id="content">
<p>hello world</p>
<p>this is a paragraph</p>
</div>

JQuery code

alert($("#content").text()); 

Result (IE) IE removes a new line (\ n), how can I fix this problem?

hello worldthis is a paragraph

Result (FF)

hello world
this is a paragraph

take a look: http://jsfiddle.net/vB3bx/

+3
source share
4 answers

Not sure if you find a solution using text () if you consider the following:

Due to differences in HTML parsers in different browsers, return text may differ in new characters and other empty space.

at http://api.jquery.com/text/

0

, div innerText.

alert($("#content").text());

alert( document.getElementById( "content" ).innerText );
+2

Internet Explorer SPACE. , .

btw, , IE9 beta . .

+2

, . , , , (innerText, innerHTML, jQuery , TextRange, pre .. ..), . , IE . , : pre , javascript , pre textarea.

In IE9, this behavior has changed. The only solution in older versions would be to replace the new line characters with tags
(or something really, a semicolon, etc.) on the server, if possible, and then replace them with \ n in javascript after extracting the text content.

+1
source

Source: https://habr.com/ru/post/1762693/


All Articles