du521521521 2016-07-04 09:11
浏览 451
已采纳

从api获取数据在页面中以HTML格式打印

  1. My data is having HTML as below
<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

  1. Fetching the data is as below

    var description=$.trim(products.description);

  2. My requirement is HTML tags should apply while displaying in page using jquery. Please help with this !!

  • 写回答

1条回答 默认 最新

  • douzhang6646 2016-07-04 09:22
    关注

    You can return the HTML by converting all applicable characters to HTML entities using htmlentities() PHP function.

    So convert your String in PHP API like this,

    $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);
    

    Above code will make the returned string like this,

    &lt;p&gt;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&lt;/p&gt;
    

    Which includes HTML entities,

    Now when you set this string as HTML on page using jQuery OR Javascript, you will able to see HTML as it is on the page.

    Reference: http://php.net/manual/en/function.htmlentities.php

    Demo: https://eval.in/600050


    If you want to set the HTML as it is on the HTML page, you can use .text() jQuery method.

    So your code would look something line this,

    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);
    

    Above code will set HTML as is, without forcing it to render HTML.

    jFiddle: https://jsfiddle.net/b3pr4qtb/


    If you want to show HTML from API, which means showing bullet list and etc..
    You can use .html() jQuery function.

    So your code should look something like this,

    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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码