【发布时间】:2014-02-24 07:39:22
【问题描述】:
我使用 getJson() 函数通过 php 从服务器动态检索内容。代码是这样的:
function loadContent(url){
$.getJSON("content.php", {cid: url, format: 'json'}, function(json) {
$.each(json, function(key, value){
$(key).html(value);
});
});
}
content.php 从 mysql 获取一些数据并生成一个数组并将其编码为 json 格式数组,然后使用 JavaScript 在 main_page.html 中打印它。我想要做的是 content.php 的输出是一个字符串(或数组中的一行)和 html 代码(结合来自 mysql 的数据)。因为我不想只打印一个数组。例如,我希望 content.php 输出是这样的:
<div id="content">
<div id="one">
some html code combined with data from mysql.
</div>
<div id="two">
some html code combined with data from mysql.
</div>
.
.
.
</div>
所有这些都打印在 main_page.html 中的 <div id="main"> ..output of content.php.. </div> 中。所以json函数不必循环这段代码
$.each(json, function(key, value){
$(key).html(value);}
并打印一份。
【问题讨论】: