2012-03-01 176 views
0

JSON看起来是这样的:如何对数组进行排序并使用HTML进行换行?

[{"title":"Modern\/Contemporary House of mine.","link":"http:\/\/buildworx-mc.com\/forum\/showthread.php?tid=1718","images":["http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/house1.png","http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House2.png","http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House3.png","http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House4.png","http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House5.png","http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House6.png"]} 

我能拿到冠军,并链接就好了。但我无法得到图像,因为在一些有多个链接。

enter image description here

我试图让每一个图像链接到被包裹在HTML。

$.getJSON("gallery/getScreenshots.php", function (data) { 
    $.each(data, function (i, image) { 
     var link = image.link, title = image.title. images = image.images; 

    $img = $('<img>').attr('src', images); 
    $('#screenshots').append($img); 

    }); 

但结果是这样的:

<img src="http://i1139.photobucket.com/albums/n555/xDJBOUTIx/house1.png,http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House2.png,http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House3.png,http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House4.png,http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House5.png,http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House6.png" alt=""> 

我如何排序整个数组,然后获得每个图像追加到<img>?

回答

2

使用$.each循环:

$.each(images, function(i,el) { 
    $('#screenshots').append($('<img>',{'src',el})); 
}); 
+0

我用你的,我得到未捕获的SyntaxError:意外的标记, – nowayyy 2012-03-01 03:12:58

+0

其实没关系,谢谢! – nowayyy 2012-03-01 06:02:53

0

地狱Trippy,

这是以下代码和工作演示here

var info=[ 
    { 
     "title": "Modern/Contemporary House of mine.", 
     "link": "http://buildworx-mc.com/forum/showthread.php?tid=1718", 
     "images": [ 
      "http://i1139.photobucket.com/albums/n555/xDJBOUTIx/house1.png", 
      "http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House2.png", 
      "http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House3.png", 
      "http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House4.png", 
      "http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House5.png", 
      "http://i1139.photobucket.com/albums/n555/xDJBOUTIx/House6.png" 
     ] 
    } 
]; 
var album=info[0]["images"]; 
for(i=0;i<album.length;i++) 
{ console.log(album[i]); 
    $('#screenshots').append($('<img>').attr('src', album[i])          
         .css({'width':'200px','height':200px'})); 
}