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/

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

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度