使用jQuery或Javascript将HTMLString转换为HTML

时间:2020-07-15 12:36:10

标签: javascript html jquery ipad uiwebview

我有一个字符串,基本上是这样的HTML字符串-

  var str =  "<section id='section1' class='class1'> <h3>Type 3 Heading</h3><p>Some text goes in <span> here</span> </p></section>"

我正在尝试使用下面的jQuery函数将此HTMLString转换为真实的HTML组件,并将其放置在div id sampleSection 这样的

$('#sampleSection').html(str);

但是它仍然作为普通字符串加载,它确实考虑了html元素,但是像这样将其放置为普通字符串-

Type 3 Heading Some text goes in here 

我想要这样-

类型3标题

有些文字在这里

注意-我正在尝试使用本地HTML和脚本文件将其加载到iPad InAppWebView上。是否有我想念的东西,还是需要以其他方式做?感谢您的帮助。

var str =  "<section id='section1' class='class1'> <h3>Type 3 Heading</h3><p>Some text goes in <span> here</span> </p></section>"
$("#sample").html(str);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="sample"></div>

1 个答案:

答案 0 :(得分:1)

问题出在h3的结束标记中。 您已将其关闭为...

var str =  "<section id='section1' class='class1'> <h3>Type 3 Heading</h3><p>Some text goes in <span> here</span> </p></section>"
相关问题