使用for循环遍历JSON

时间:2016-04-04 13:02:45

标签: javascript jquery

我正在尝试按照JSON添加图片 -

json = [{
        "id": 6,
        "name": "rac2 ",
        "shop_id": 4,
        "categories_id": 5,
        "category": "category-5",
        "min_price": 555,
        "max_price": 55656,
        "image": "http://www.shopforshops.com/www/61/files/clothing-racks1.jpg",
    },
    {
        "id": 64,
        "name": "rac32 ",
        "shop_id": 4,
        "categories_id": 5,
        "category": "category-5",
        "min_price": 555,
        "max_price": 55656,
        "image": "http://ohjoy.blogs.com/.a/6a00d8341c6a0853ef019affb2bbfe970c-800wi",
    },
    {
        "id": 643,
        "name": "rac32 ",
        "shop_id": 44,
        "categories_id": 5,
        "category": "category-5",
        "min_price": 555,
        "max_price": 55656,
        "image": "http://cache2.asset-cache.net/gc/129305805-racks-of-clothes-in-a-menswear-store-gettyimages.jpg?v=1&c=IWSAsset&k=2&d=mcPYJ5XyANr2jzh2gHmNDG%2FS5MzSDogxQ%2BKrWHyAnTHGKr%2FOnkBx3JkVNAMcV9GZ",
    },
]

我正在循环它 -

 $('.rackLayer').css({"background-image": "url('" + json[0].image + "')"});
            for (z = 1; z < json.length; z++)
            {
                    $('.rackLayer').after("<div class='inside-rack'> </div>");
                    $('.inside-rack').css({"background-image": "url('" + json[z].image + "')"});
            }

和html是 -

<div class="container-fluid">
            <div class="row">
                <div id="scroll-section">
                    <div class="rackLayer">

                    </div>
                </div>
            </div>
        </div>

我想要的是应该首先添加带有O索引的图像,然后按照索引1然后按2的顺序添加剩余图像。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

问题是$('.inside-rack').css({"background-image": "url('" + json[z].image + "')"});将背景图像应用于所有先前添加的元素,而只需要定位新创建的元素。

&#13;
&#13;
json = [{
  "id": 6,
  "name": "rac2 ",
  "shop_id": 4,
  "categories_id": 5,
  "category": "category-5",
  "min_price": 555,
  "max_price": 55656,
  "image": "http://www.shopforshops.com/www/61/files/clothing-racks1.jpg",
}, {
  "id": 64,
  "name": "rac32 ",
  "shop_id": 4,
  "categories_id": 5,
  "category": "category-5",
  "min_price": 555,
  "max_price": 55656,
  "image": "http://ohjoy.blogs.com/.a/6a00d8341c6a0853ef019affb2bbfe970c-800wi",
}, {
  "id": 643,
  "name": "rac32 ",
  "shop_id": 44,
  "categories_id": 5,
  "category": "category-5",
  "min_price": 555,
  "max_price": 55656,
  "image": "http://cache2.asset-cache.net/gc/129305805-racks-of-clothes-in-a-menswear-store-gettyimages.jpg?v=1&c=IWSAsset&k=2&d=mcPYJ5XyANr2jzh2gHmNDG%2FS5MzSDogxQ%2BKrWHyAnTHGKr%2FOnkBx3JkVNAMcV9GZ",
}]


var $layer = $('.rackLayer').css({
  "background-image": "url('" + json[0].image + "')"
});

for (z = 1; z < json.length; z++) {
  $("<div class='inside-rack'></div>").css({
    "background-image": "url('" + json[z].image + "')"
  }).insertAfter($layer);
  $('.rackLayer').after();
}
&#13;
.rackLayer,
.inside-rack {
  height: 150px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rackLayer"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

更新到这个:

for (z = 1; z < json.length; z++)
{
   var div = $("<div >", { //<---creates div element
      class  :'inside-rack', 
      'style':'background-image:url("'+ json[z].image +'")' // <--style the bgimg
   });
   $('.rackLayer').after(div); //<---now push in the DOM.
}

问题在我看来,选择器,$('.inside-rack')这将改变所有div。当循环结束时,每个从json获取最后一个对象的图像属性 因此,解决方案是在那里创建一个新的元素样式,然后将其推送到DOM中。