嗨,大家好我試圖從JSON文件加載數據,我想用它作爲背景爲每一個獲得惠譽如何加載特定的數據從JSON文件顏色
塊,塊是一個盒子,每個盒子都有4個小boxs和我想針對這些4個boxs並基於JSON文件數據
這裏是我使用
[
{
"name" : "color1" ,
"box1" : "#7f6ded" ,
"box2" : "#343434" ,
"box3" : "#ffffff" ,
"box4" : "#858585"
} ,
{
"name" : "color2" ,
"box1" : "#58c9b9" ,
"box2" : "#343434" ,
"box3" : "#ffffff" ,
"box4" : "#9dc8c8"
}
]
的JSON格式和這裏有背景色是調用JQ
$(function() {
// start calling colors
$.getJSON("js/colors.json", function(data) {
// console.log(data[1].box1);
var output = "";
var output2 = "";
for (var i = 0; i < data.length; i++) {
output +=
"<div class=" + "col-md-4" + ">" +
"<div class=" + "co-palet" + ">" +
"<div class=" + "box box-1" + "></div>" +
"<div class=" + "box box-2" + "></div>" +
"<div class=" + "box box-3" + "></div>" +
"<div class=" + "box box-4" + "></div>"
+ "</div>" + "</div>"
$('#myColors').html(output);
};
});
// end calling colors
});
和I輸出到
<div id="myColors"></div>
任何幫助,將不勝感激:)
inside for loop'alert(data [i] .name);' –