jquery解码无法正常工作

时间:2017-10-12 08:15:11

标签: jquery

我有一个字符串,其中包含一些html entites和一些像这样的HTML标签

var entity = '<strong>kjkkkjk<br><br><br></strong>&lt;iframe width="854" height="480" src="https://www.youtube.com/embed/-FSjaaibpus" frameborder="0" allowfullscreen=""&gt;&lt;/iframe>';

var decode = $('<div></div>').html(entity).text();
console.log(decode);

它给出了这个不合适的结果

kjkkkjk<iframe width="854" height="480" src="https://www.youtube.com/embed/-FSjaaibpus" frameborder="0" allowfullscreen=""></iframe>

期望的结果应该是这样的

<strong>kjkkkjk<br><br><br></strong><iframe width="854" height="480" src="https://www.youtube.com/embed/-FSjaaibpus" frameborder="0" allowfullscreen=""></iframe>
我出错了哪里?如何获得正确的结果?

1 个答案:

答案 0 :(得分:0)

像这样使用'html':

var entity = '<strong>kjkkkjk<br><br><br></strong>&lt;iframe width="854" height="480" src="https://www.youtube.com/embed/-FSjaaibpus" frameborder="0" allowfullscreen=""&gt;&lt;/iframe>';

  var decode_text = $('<div></div>').html(entity).text();
  var decode_html = $('<div></div>').html(entity).html();
  console.log(decode_text);
  console.log(decode_html);