<p> Are you looking for a camera that will help you capture memorable moments with high clarity? If so, then Nikon D3200 24.2 MP
数码单反相机就是答案
获取数据如下
var description = $ .trim(products.description);
我的要求是在使用jquery在页面中显示时应该应用HTML标记。请帮忙!!
答案 0 :(得分:0)
您可以使用htmlentities() PHP函数将所有适用的字符转换为HTML实体来返回HTML。
所以在 PHP API中转换你的字符串,就像这样,
$string="<p>Are you looking for a camera that will help you capture memorable moments with high clarity? If so, then Nikon D3200 24.2 MP Digital SLR Camera is the answer</p>";
$string=htmlentities($string);
上面的代码将使返回的字符串像这样,
<p>Are you looking for a camera that will help you capture memorable moments with high clarity? If so, then Nikon D3200 24.2 MP Digital SLR Camera is the answer</p>
其中包括 HTML实体,
现在,当您使用jQuery或Javascript在页面上将此字符串设置为HTML时,您将能够看到页面上的HTML。
参考:http://php.net/manual/en/function.htmlentities.php
如果要在HTML页面上设置HTML,可以使用.text() jQuery方法。
所以你的代码会看起来像这样,
var someHtmlString = "<p>Are you looking for a camera that will help you capture memorable moments with high clarity? If so, then Nikon D3200 24.2 MP Digital SLR Camera is the answer</p>";
$("div#DIV_ID").text(someHtmlString);
上面的代码会按原样设置 HTML ,而不会强制它呈现HTML。
jFiddle :https://jsfiddle.net/b3pr4qtb/
如果您想从API中显示 HTML ,这意味着显示项目符号列表等。
您可以使用.html() jQuery函数。
所以你的代码看起来应该是这样的,
var someHtmlString = "<ul><li>Are you looking for a camera that will help you capture memorable moments with high clarity? If so, then Nikon D3200 24.2 MP Digital SLR Camera is the answer</li></ul>";
$("div#DIV_ID").html(someHtmlString);
jFiddle: https://jsfiddle.net/b3pr4qtb/1/