我有一个 JSON 对象,其中包含以下示例条目:
description":"<div>- diversen Diskutanten- Moderiert vom gro\u00dfartigen PreibischDebatte, Gesprochenes, Kontroverses<div class="btn-group"><a class"btn" href=""><i class="icon-map-marker"><\/i><\/a><a class"btn" href=""><i class="icon-map-marker"><\/i><\/a><\/div><\/div>"
现在我正尝试将此 html 添加到我的代码中的另一个元素:
$('#desc_text').empty();
$('#desc_text').html(event.description);
$('#description').modal('show');
问题是 html 没有被解析为 html,我认为 jquery .html()
会这样做。我也尝试使用 append()
,但也没有改变。
有谁知道如何将其解析为 html?
编辑 2:整个工作流程,因为它看起来有点困惑:
首先我做的是:
$events = json_encode($func->getAllDates());
函数的作用是:
public function getAllDates() {
$this->db->query('SELECT * FROM dates ORDER BY start');
$result = $this->db->mysql_fetch_all();
for($i = 0; $i < count($result); $i++) {
$result[$i]['description'] = html_entity_decode(utf8_encode($result[$i]['description']));
}
print_r($result);
return $result;
}
$result函数的输出:
[6] => Array
(
[id] => 17
[title] => Sabotage Debatte: Politik im Keller
[start] => 2012-11-29 00:00:00
[end] => 2012-11-29 00:00:00
[allDay] => 1
[url] =>
[description] => <div>- diversen Diskutanten- Moderiert vom großartigen PreibischDebatte, Gesprochenes, Kontroverses<div class="btn-group"><a class"btn" href=""><i class="icon-map-marker"></i></a><a class"btn" href=""><i class="icon-map-marker"></i></a></div></div>
)
编辑:
正如评论中所说:引用似乎是答案,但我不自己引用,只是使用 php-functions。也许我的做法是错误的。
我从我的数据库中得到了这样的东西:
&lt;div&gt;- diversen Diskutanten- Moderiert vom großartigen PreibischDebatte, Gesprochenes, Kontroverses&lt;div class=&quot;btn-group&quot;&gt;&lt;a class&quot;btn&quot; href=&quot;&quot;&gt;&lt;i class=&quot;icon-map-marker&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;a class&quot;btn&quot; href=&quot;&quot;&gt;&lt;i class=&quot;icon-map-marker&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;
然后我用
html_entity_decode(utf8_encode($result[$i]['description']));
将其转回,然后我使用 json_encode 将其转换为 json 格式。这些函数中的任何一个都不应该是正确的引用吗?
我希望您了解工作流程...
最佳答案
您的字符串包含必须转义的内部引号。此外,您还有一些错误的属性,例如 class"btn"
应该是 class="btn"
之后,它将正常工作。
// description with all internal double quotes escaped by \"
var event = {
"description":"<div>- diversen Diskutanten- Moderiert vom gro\u00dfartigen PreibischDebatte, Gesprochenes, Kontroverses<div class=\"btn-group\"><a class=\"btn\" href=\"\"><i class=\"icon-map-marker\"><\/i><\/a><a class=\"btn\" href=\"\"><i class=\"icon-map-marker\"><\/i><\/a><\/div><\/div>"
};
$('#desc_text').empty();
$('#desc_text').html(event.description);
Here is an example on jsfiddle
OP评论后更新:
由于您正在检索此值并对实体(包括引号)进行解码,因此您必须 json_encode()
它以生成正确转义的引号。
json_encode(html_entity_decode(utf8_encode($result[$i]['description'])));
或者更可能的是,对它来自的整个数组进行编码:
json_encode($your_database_row_array);
关于php - jQuery 从 JSON 字符串创建 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13626816/