jQuery text() function loses line breaks in IE

后端 未结 2 937
囚心锁ツ
囚心锁ツ 2020-12-18 03:04

This is quite a talked about problem on the jQuery forums but I can\'t figure out a solution for my situation.

I have an unordered list with some text elements in th

2条回答
  •  死守一世寂寞
    2020-12-18 03:43

    Here is a far simpler solution to convert any HTML into plain text with line breaks based on the use of
    or

    tags.

    function htmlDecodeWithLineBreaks(html) {
      var breakToken = '_______break_______',
          lineBreakedHtml = html.replace(//gi, breakToken).replace(/(.*?)<\/p>/gi, breakToken + '$1' + breakToken);
      return $('
    ').html(lineBreakedHtml).text().replace(new RegExp(breakToken, 'g'), '\n'); }

    For example, calling the following method:

    htmlDecodeWithLineBreaks('line 1
    line & 2
    ' + 'line > 3

    paragraph 1

    line 4')

    returns:

    line 1
    line & 2
    line > 3
    paragraph 1
    line 4
    

    Hope that helps ;)

提交回复
热议问题