Javascript 从JSON结果更新HTML内容
我有以下HTML代码:Javascript 从JSON结果更新HTML内容,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我有以下HTML代码: <tr class="gris"> <td width="6%" class="titulo"><a href="{{ Here goes some URL}}"><img src="assets/play_icon.png" width="32" height="32" /></a></td> <td width="60%" class="titulo"><a hr
<tr class="gris">
<td width="6%" class="titulo"><a href="{{ Here goes some URL}}"><img src="assets/play_icon.png" width="32" height="32" /></a></td>
<td width="60%" class="titulo"><a href="{{ Here goes some URL }}">{{ Here goes some title }}</a> <span></br>Descripción de la Pista o Podcast</span></td>
<td width="17%" class="fecha">{{ Here goes some date}}</td>
<td width="17%" class="tiempo">{{ Here goes some time}}</td>
</tr>
我应该能够生成与JSON上的值一样多的TR
,当然可以替换生成的TR
中正确的值。例如,对于每个迭代,在示例代码中我有{{这里有一些URL}}
我应该从JSON响应中写入URL
值
更新
我尝试了以下代码:
$(function() {
window.setInterval(function() {
var request = $.ajax({
type: 'GET',
url: "http://devserver/reader/podcast/podcast.php",
success: function(data) {
$("#podlist").html();
if (data.response === false) {
$("#podlist").html(data.error);
} else {
console.log(data.html_content);
if (data.html_content.length != 0) {
var htm = null;
for (var i in data.html_content) {
var jsonObj = data.html_content[i];
htm += "<tr class='gris'><td width='6%' class='titulo'><a href='" + jsonObj.url + "'><img src='assets/play_icon.png' width='32' height='32' /></a></td><td width='60%' class='titulo'><a href='" + jsonObj.url + "'>" + jsonObj.title + "</a> <span></br>" + jsonObj.description + "</span></td><td width='17%' class='fecha'>" + jsonObj.date + "</td><td width='17%' class='tiempo'>" + jsonObj.hour + "</td></tr>";
}
$("#podlist").append(htm);
}
}
},
error: function() {
request.abort();
}
});
}, 5000);
});
只用
var myhtml = '<tr class="gris"><td width="6%" class="titulo"><a href="{{ Here goes some URL}}"<img src="assets/play_icon.png" width="32" height="32" /></a></td><td width="60%" class="titulo"><a href="{{ Here goes some URL }}">{{ Here goes some title }</a><span></br>Descripción de la Pista o Podcast</span></td><td width="17%" class="fecha">{{ Here goes some date}}</td><td width="17%" class="tiempo">{{ Here goes some time}}</td></tr>';
$.each(the_data,function(i , v) {
$('.titulo a' , myhtml).attr('href' , v.url);
// and so on
$('.conatiner').append(myhtml);
})
var myhtml='descriptionón de la Pista o Podcast{{{某个日期到了}}{{某个时间到了}}';
$.each(_数据,函数(i,v){
$('.titulo a',myhtml).attr('href',v.url);
//等等
$('.conatiner').append(myhtml);
})
您可以尝试获取table元素,清除其内部HTML,然后在$中。响应的每个循环(它是一个列表)都会附加所需的行。如果“行”计数是动态的,并且您只有在异步请求后才知道它,那么这是最好的方法。尝试这样做
控制台日志(数据)
Ajax调用是否返回数据
f12并设置一个断点,或使用所选浏览器中的“net”面板查看Ajax调用的结果。与此格式类似。你得努力一点。:) 从这里核对答案。。
简单的解决方案是将模板维护为
var template = '<tr class="gris">'+
'<td width="6%" class="titulo"><a href="{{ Here goes some URL}}"><img src="assets/play_icon.png" width="32" height="32" /></a></td>'+
'<td width="60%" class="titulo"><a href="{{ Here goes some URL }}">{{ Here goes some title }}</a> <span></br>Descripción de la Pista o Podcast</span></td>'+
'<td width="17%" class="fecha">{{ Here goes some date}}</td>'+
'<td width="17%" class="tiempo">{{ Here goes some time}}</td>'+
'</tr>';
var jstring = jQuery.parseJSON(jsonString);
var htmlContent = jstring.html_content;
var i=0;
var t =template;
for(;i<htmlContent.length;i++){
t = t.replace("{{ Here goes some URL}}", htmlContent[i].url);
//Similarly do for the other values...
jQuery("table").append(jQuery(t));
t=template;
}
var模板=“”+
''+
“请描述Pista o Podcast的主题”+
“{{有个约会}}”+
“{{有时间了}}”+
'';
var jstring=jQuery.parseJSON(jsonString);
var htmlContent=jstring.html\u content;
var i=0;
var t=模板;
对于(;我希望您正在谈论使用AJAX动态“从JSON结果更新HTML内容”,我提出了以下解决方案。只需在AJAX的成功回调中编写以下代码:
if(responseData.html_content.length != 0) {
var htm = null;
for(var i in responseData.html_content) {
var jsonObj = responseData.html_content[i];
htm += "<tr class='gris'><td width='6%' class='titulo'><a href='"+jsonObj.url+"'><img src='assets/play_icon.png' width='32' height='32' /></a></td><td width='60%' class='titulo'><a href='"+jsonObj.url+"'>"+jsonObj.title+"</a> <span></br>"+jsonObj.description+"</span></td><td width='17%' class='fecha'>"+jsonObj.date+"</td><td width='17%' class='tiempo'>"+jsonObj.hour+"</td></tr>";
}
$("#table-id").append(htm);
}
if(responseData.html\u content.length!=0){
var htm=null;
for(responseData.html\u内容中的变量i){
var jsonObj=responseData.html_content[i];
htm+=“”+jsonObj.description+“”+jsonObj.date+“”+jsonObj.hour+“”;
}
$(“#表id”).append(htm);
}
希望这有帮助!你使用的是返回JSON的AJAX吗?是的,我使用的是AJAX,我不好,更不用说以前我尝试了你的代码,我得到了html\u内容
未定义,JSON响应与主帖子相同,不知道它在哪里失败,你在$.AJAX方法中提到了什么内容类型和数据类型?别担心,我添加了dataType:json
现在代码可以按照我的要求工作了,我只是有一个疑问:我怎么能维护现有的值并只添加新的值呢?正如你所说的,每次迭代都会添加新的条目,所以我将从append
更改为html
,但如果查询返回300个值,第二次返回600个值呢?我只想添加新的300个值和保留现有的300,明白吗?您将不得不使用.append而不是.html,因为.html将用新元素替换现有的html元素,而.append将保留现有的元素-顾名思义。
$.post('test.php', { id: id },
function (page) {
$('.titulo a' , myhtml).attr('href' , page.html_content..url);
$('.conatiner').append(page.html_content.data);
}, 'json');
var template = '<tr class="gris">'+
'<td width="6%" class="titulo"><a href="{{ Here goes some URL}}"><img src="assets/play_icon.png" width="32" height="32" /></a></td>'+
'<td width="60%" class="titulo"><a href="{{ Here goes some URL }}">{{ Here goes some title }}</a> <span></br>Descripción de la Pista o Podcast</span></td>'+
'<td width="17%" class="fecha">{{ Here goes some date}}</td>'+
'<td width="17%" class="tiempo">{{ Here goes some time}}</td>'+
'</tr>';
var jstring = jQuery.parseJSON(jsonString);
var htmlContent = jstring.html_content;
var i=0;
var t =template;
for(;i<htmlContent.length;i++){
t = t.replace("{{ Here goes some URL}}", htmlContent[i].url);
//Similarly do for the other values...
jQuery("table").append(jQuery(t));
t=template;
}
if(responseData.html_content.length != 0) {
var htm = null;
for(var i in responseData.html_content) {
var jsonObj = responseData.html_content[i];
htm += "<tr class='gris'><td width='6%' class='titulo'><a href='"+jsonObj.url+"'><img src='assets/play_icon.png' width='32' height='32' /></a></td><td width='60%' class='titulo'><a href='"+jsonObj.url+"'>"+jsonObj.title+"</a> <span></br>"+jsonObj.description+"</span></td><td width='17%' class='fecha'>"+jsonObj.date+"</td><td width='17%' class='tiempo'>"+jsonObj.hour+"</td></tr>";
}
$("#table-id").append(htm);
}