【问题标题】:Printing html from JSON从 JSON 打印 html
【发布时间】:2018-06-11 18:06:50
【问题描述】:

只是寻求帮助。我在 JSON 中有 html 标签。但是当我尝试打印它时,标签只是打印为字符串而不被解释为 HTML。例如 或

使用 PHP 时,我会对其进行解码,然后按预期打印。我该怎么做才能通过 jquery 将其附加到元素?

示例代码

response = { 
  "title": "<bold>This is the title</bold><br>Next is the body<br>" 
};
$('#title').text(response.title); 

谢谢

【问题讨论】:

  • text() 输出就是这样。请改用html()
  • .html() 而不是 .text()。但要注意内容! &lt;script&gt; 标签也会在插入时执行。
  • 干杯,伙计们。如此简单......我想我必须做一些解码。
  • 您需要知道您在文本中提到的 HTML 是无效的。没有 &lt;bold&gt;&lt;/bold&gt;&lt;/br&gt; 这样的东西。

标签: php jquery json


【解决方案1】:

只需使用.html() 函数而不是.text()

response = { 
  "title": "<bold>This is the title</bold><br>Next is the body<br>" 
};
$('#title').html(response.title); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="title"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 2014-05-17
    • 2016-10-03
    相关资源
    最近更新 更多