我试图将数据添加到一个空的var,但数据没有被存储。 致电hotel_list
时,显示为空。我错过了什么?谢谢!数据没有存储在变量中
var hotelData = JSON.parse(data);
function hotelList(){
var hotel_list = '';
for (i = 0; i < hotelData.length; i++) {
function list(index, hotel) {
hotel_list += '<li class="hotel" data-index="'+ index +'">'+ hotel.name +'</li>';
document.getElementById("hotel-listing").innerHTML = hotel_list;
};
};
};
document.addEventListener('DOMContentLoaded', function() {
hotelList();
});
你定义一个函数内循环,但从来没有调用它。 –
For循环不使用回调。你对'each'还是'forEach'感到困惑? – JJJ
可能我想迭代hotelData,但我不知道如何将(索引,酒店)传递给forEach循环 – Gibson