首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于.html(),jQuery / json循环没有给出完整的结果

对于.html(),jQuery / json循环没有给出完整的结果
EN

Stack Overflow用户
提问于 2012-08-30 18:34:38
回答 3查看 151关注 0票数 0
代码语言:javascript
复制
// Calling the video function with JSON

$.getJSON("videos.php", function(data){
// first check if there is a member available to display,
//if not then show error message
    if(data == '') { 
        $('#tabs-4').html("<div class='errorMember'>Sorry, there is currently no member available videos</div>");
    } 
    // if there is a member, then loop through each data available
    else {
          $.each(data, function(i,name){
            content = '<div class="left"><img src="' + name.pic + '"/>';
            content += '<p>' + name.name + '</p>';
            content += '<a href="' + name.link + '" target="_blank">Video link</a>';
            content += '</div><br/><hr>';
            $("#tabs-4").html(content);
        });
    }
});​

问题是它只给我一个结果,而不是数组中的结果列表,但是如果我appendTo(content) ..它在当前下添加了完整的结果列表,这不是我想要的,因为我需要用更新后的数据刷新该内容。

你知道我做错了什么吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-30 18:44:27

在填充之前清空元素:

代码语言:javascript
复制
$('#tabs-4').empty();
$.each(data, function(i,name){
  var content =
    '<div class="left"><img src="' + name.pic + '"/>' +
    '<p>' + name.name + '</p>' +
    '<a href="' + name.link + '" target="_blank">Video link</a>' +
    '</div><br/><hr>';
  $("#tabs-4").append(content);
});

或者在将字符串放入元素之前将所有元素放入其中:

代码语言:javascript
复制
var content = '';
$.each(data, function(i,name){
  content +=
    '<div class="left"><img src="' + name.pic + '"/>' +
    '<p>' + name.name + '</p>' +
    '<a href="' + name.link + '" target="_blank">Video link</a>' +
    '</div><br/><hr>';
});
$("#tabs-4").html(content);
票数 0
EN

Stack Overflow用户

发布于 2012-08-30 18:52:34

到目前为止,可能只显示了最后一个元素。

代码语言:javascript
复制
// Calling the video function with JSON

$.getJSON("videos.php", function(data){

    // first check if there is a member available to display, if not then show error message
    if(data == '') { 
        $('#tabs-4').html("<div class='errorMember'>Sorry, there is currently no member available videos</div>");
    } 
    // if there is a member, then loop through each data available
    else {
        //If you want to Clear the Container html
        $("#tabs-4").html(''); 
        $.each(data, function(i,name){
            content = '<div class="left"><img src="' + name.pic + '"/>';
            content += '<p>' + name.name + '</p>';
            content += '<a href="' + name.link + '" target="_blank">Video link</a>';
            content += '</div><br/><hr>';
            $("#tabs-4").append(content);
        });
    }
});
票数 1
EN

Stack Overflow用户

发布于 2012-08-30 18:37:42

如果我很理解的话,你可能想做这样的事情。

代码语言:javascript
复制
...
else {
        $("#tabs-4").empty(); // remove previous data (if any)

        $.each(data, function(i,name){
            content = '<div class="left"><img src="' + name.pic + '"/>';
            content += '<p>' + name.name + '</p>';
            content += '<a href="' + name.link + '" target="_blank">Video link</a>';
            content += '</div><br/><hr>';

            $("#tabs-4").append(content); // append new data
        });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12194916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档