我有一个JSON文件,其中包含8张图像,每个图像我都希望与一个类一起显示。我试图将第一张图片加载到第一类上,但是它不起作用。
我尝试过的jQuery:
jQuery(function($) {
$.getJSON('example.json',function (response) {
$('.tile-image1').each(function(i) {
$(this).append('<div><img src= "' + response.Tiles[i].img[i].img1 + '">
</div>');
});
});
});
HTML:
<div class="tile-image1"></div>
<div class="tile-image2"></div>
<div class="tile-image3"></div>
<div class="tile-image4"></div>
JSON:
{
"Tiles" : [
{
"title" : "","year" : "-","category" : "","img": {
"img1" : "example.jpg","img2" : "example.jpg","img3" : "example.jpg","img4" : "example.jpg"
}
},{
"title" : "","year" : " -","img" : {
"img1" : "example.jpg","img4" : "example.jpg"
}
}
]
}
因此,我想要实现的是与类tile-image1一起显示的JSON文件中的所有“ img1”,对于类tile-image2的所有“ img2”,等等。这和任何帮助将不胜感激。