jquery - 清空之前的数据后无法显示预测数据
问题描述
我试图显示 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));
}
}
有人可以帮忙吗?
解决方案
您正在循环播放 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);
}
}
推荐阅读
- django - 管理站点显示时区错误
- java - Axon MongoDB - message='E11000 重复键错误集合 uniqueAggregateIndex dup key: { : "101", : 0 }
- sql-server - 即使在声明标量变量后我也收到此错误“必须声明标量变量“@col_shipping_price”
- c++ - 在 [a,b] 区间之间找到相同的数字
- sphinx - 狮身人面像错误: (type='index') 已经存在
- mariadb - MariaDB 为每个 find_in_set 值选择记录
- matrix - 如何使用颤振执行有关缩放,旋转和平移使用矩阵的功能
- c - 在使用诸如 C 之类的编程语言的某些软件中是否有间接寻址的实际应用?
- numpy - 如何根据值标记 numpy 数组?
- python-2.7 - 使用“with”方法在 Python 中保存文件