如何在动态添加内容时将html shorthand 更改为html 标签

How to change html shorthand to html tag when adding content dynamically

var encodedHtml = "<ol style="color: rgb(34, 34, 34); font-family: arial, sans-serif;"><li class="mod" style=""> <div class="_oDd" style=""><span class="_Tgc" style="font-size: 16px;"><b>Test data</b> is <b>data</b>

已明确用于 <b>tests</b>,通常用于计算机程序。某些 <b>data</b> 可用于确认方式,通常用于验证给定函数的给定输入集是否会产生某些预期结果。</span></div> </li></ol>";

我想将此内容更改为 html 标签,这样我就可以使用 js 将其添加到某些 div。

这应该可以帮助您解决问题:

What's the right way to decode a string that has special HTML entities in it?

下面的几个答案有一个 jQuery 版本:

function htmlDecode(value) {
    return $("<textarea/>").html(value).text();
}

var encodedHTML = "&lt;ol style=&quot;color: rgb(34, 34, 34); font-family: arial, sans-serif;&quot;&gt;&lt;li class=&quot;mod&quot; style=&quot;&quot;&gt; &lt;div class=&quot;_oDd&quot; style=&quot;&quot;&gt;&lt;span class=&quot;_Tgc&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;Test data&lt;/b&gt; is &lt;b&gt;data&lt;/b&gt; which has been specifically identified for use in &lt;b&gt;tests&lt;/b&gt;, typically of a computer program. Some &lt;b&gt;data&lt;/b&gt; may be used in a confirmatory way, typically to verify that a given set of input to a given function produces some expected result.&lt;/span&gt;&lt;/div&gt; &lt;/li&gt;&lt;/ol&gt;";

$("<p/>").html(encodedHTML ).text();

这也行。