我想从 php 脚本中返回带有 json 的数据。数据包含html标签,但是当我尝试将数据用作html时,我可以看到"标签"。
HTML 标记将显示为:<h1>hello</h1>
和 not:
hello
我在聚合物中使用<core-ajax>
标签
<core-ajax id="ajax" auto url="php/directory_json.php" on-core-response="{{docsLoaded}}" handleAs="json"></core-ajax>
爪哇语
var template = document.querySelector('template[is="auto-binding"]');
var docs = []
template.docsLoaded = function() {
this.docs = this.$.ajax.response.slice(0);
}
.HTML
<template repeat="{{doc in docs}}"><div>{{doc.content}}</div></template>
.PHP
$my_html = array(array('uid'=>'0', 'content'=>'<h1>hello</h1>'));
echo json_encode($my_html);
任何帮助将不胜感激
编辑:似乎你不能只用html代码放一个变量。您需要使用 innerHTML()
将其应用于元素中($.html()
使用 jquery)。
现在我偶然发现了另一个问题,我不能用它的 ID 定位我的元素,它说"未定义"。
.HTML
<template repeat="{{doc in docs}}">
<div id="div-content-"{{doc.uid}}></div>
{{myfunction(doc.uid, doc.content)}}
</template>
JavaScript
myfunction = function(id, content){
$('#div-content-'+id).html(content);
}
似乎在整个循环完成之前,该元素不会"应用"到 html?关于如何实现这一目标的任何想法?
您可以使用 htmlentities 函数:
$my_html = array(array('uid'=>'0', 'content'=>htmlentities('<h1>hello</h1>')));
这样,<
标签打开器将变得<
并且不会被解释