Line breaks in jQuery ajax html callback cause errors

前端 未结 2 1480
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 23:36

I am returning a large chunk of HTML from an $.ajax call. The string coming from PHP has two line breaks at the beginning, e.g.

$data = \"

<         


        
相关标签:
2条回答
  • 2021-01-13 00:23

    My HTML strings did not have any top-level text nodes, or so I thought... I found the issue was just extra whitespace. This has worked for me, and feels better than filtering out the empty nodes later:

    $($.parseHTML($.trim(html_string)))
    
    0 讨论(0)
  • 2021-01-13 00:31

    The problem seems to be caused by the text node in the collection you can filter it out with .filter('*')

    var $html = $($.parseHTML(data)).filter('*');
    
    0 讨论(0)
提交回复
热议问题