数组 while 循环
2015-03-15
62
我尝试在调用函数时使用 while 循环显示数组中的值。它将显示除图片之外的第一个对象,而不显示其他两个对象
function products(){
var txt=""
var appliance = 0
var products = new Array();
products[0] = {name: "refrigerator" , price:88.99,
img:"img/refrigerator.jpg"};
products[1] = {name: "microwave oven" , price: 76.99 , img:"img/microwave.png"};
products[2] = {name: "dishwasher" , price:276.67 , img:"img/dishwasher.jpg"};
/*var appliance = products[x].name + " " + products[x].price + " '" + products[x].img + "'>"*/
while(appliance < 3){
txt +=products[appliance].name + ' ' + products[appliance].price +" <img src='"+ products[appliance].img;"'>";
appliance++;
}
document.getElementById("answer").innerHTML = txt
}
}
3个回答
products[appliance].name + ' ' + products[appliance].price + products[appliance].img;
否则,您将尝试显示一个数组。
请注意,您忘记了
>
:
<img src=img/refrigerator.jpg
kemicofa ghost
2015-03-15
txt += products[ appliance ].name + " " + products[ appliance ].price + " '" + products[ appliance ].img + "'>" +" <br />";
ch271828n
2015-03-15
function products(){
var txt=""
var appliance = 0
var products = new Array();
products[0] = {name: "refrigerator" , price:88.99, img:"img/refrigerator.jpg"};
products[1] = {name: "microwave oven" , price: 76.99 , img:"img/microwave.jpg"};
products[2] = {name: "dishwasher" , price:276.67 , img:"img/dishwasher.jpg"};
while(appliance < 3){
txt +=products[appliance].name + ' ' + products[appliance].price +" <img src='"+ products[appliance].img + "'>" +" <br />";
appliance++;
}
document.getElementById("answer").innerHTML = txt
}
}
像老板一样解决我自己的代码
Hot Java
2015-03-15