清空先前的数据后无法显示预测数据

我试图显示5天的预测,但是每次清空前一次预测的数据时,它仅显示5天预测的最后一天的数据。我尝试将.empty()方法放在任何地方,但似乎没有任何效果。

$.ajax({
  url: queryURLThree,method: "GET"
 }).then(function (forecastResT) {

  for (var i = 0; i < forecastResT.list.length; i++) {
    if (forecastResT.list[i].dt_txt.indexOf("18:00:00") !== -1) {
      var day = forecastResT.list[i].dt_txt.split(" ")  
      var forecastNameS = $('<div class="col"><p>' + day[0] + '</p>')
      // add icon logos based on their weather conditions

     var imgOne = $("<img>").attr("src","http://openweathermap.org/img/w/" + forecastResT.list[i].weather[0].icon + ".png");
     var tempFiveone = $('<div id = "tempFive">'+ 'Temperature: ' +forecastResT.list[i].main.temp + '<div>');
     var humFiveone = $('<div id = "humFive"> '+'Humidity: ' + forecastResT.list[i].main.humidity + '<div>');
     $('#fiveDay').empty();
     forecastNameS.append(imgOne);
     forecastNameS.append(tempFiveone);
     forecastNameS.append(humFiveone);
     ($('#fiveDay').append(forecastNameS));
  }
}

有人可以帮忙吗?

zhuzheqi2096409 回答:清空先前的数据后无法显示预测数据

您要遍历5个新闻日,并每次使用empty()。这意味着您将在添加下一天后立即删除前一天,而仅保留最后一天。

在循环之前使用empty()

$('#fiveDay').empty();
for (var i = 0; i < forecastResT.list.length; i++) {
  if (forecastResT.list[i].dt_txt.indexOf("18:00:00") !== -1) {
    var day = forecastResT.list[i].dt_txt.split(" ")  
    var forecastNameS = $('<div class="col"><p>' + day[0] + '</p>')
    // add icon logos based on their weather conditions

    var imgOne = $("<img>").attr("src","http://openweathermap.org/img/w/" + forecastResT.list[i].weather[0].icon + ".png");
    var tempFiveOne = $('<div id = "tempFive">'+ 'Temperature: ' +forecastResT.list[i].main.temp + '<div>');
    var humFiveOne = $('<div id = "humFive"> '+'Humidity: ' + forecastResT.list[i].main.humidity + '<div>');

    forecastNameS.append(imgOne);
    forecastNameS.append(tempFiveOne);
    forecastNameS.append(
    $('#fiveDay').append(forecastNameS);
  }
}
本文链接:https://www.f2er.com/3133574.html

大家都在问