使用“simpleweatherjs”API显示相应的温度徽标

时间:2017-07-25 14:25:26

标签: javascript jquery weather weather-api

我依靠这个例子:https://codepen.io/fleeting/pen/xklfq我希望在接下来几天显示徽标,我试图添加此代码以显示接下来的几天温度:

for(var i=0;i<weather.forecast.length;i++) {
    html += '<p class="days">'+'<span class="d">'+weather.forecast[i].day+'</span>'+'<span class="val">'+weather.forecast[i].high+'</span>'+'</p>';
}

我展示了他们,但我不知道如何实施徽标?

完整的 html 代码:

$(document).ready(function() {
  $.simpleWeather({
    location: 'Austin, TX',
    woeid: '',
    unit: 'f',
    success: function(weather) {
      html = '<h2><i class="icon-'+weather.code+'"></i> '+weather.temp+'&deg;'+weather.units.temp+'</h2>';
      html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
      html += '<li class="currently">'+weather.currently+'</li>';
      html += '<li>'+weather.wind.direction+' '+weather.wind.speed+' '+weather.units.speed+'</li></ul>';

      //My add here
      for(var i=0;i<weather.forecast.length;i++) {
          html += '<p class="days">'+'<span class="d">'+weather.forecast[i].day+'</span>'+'<span class="val">'+weather.forecast[i].high+'</span>'+'</p>';
      }

      $("#weather").html(html);
    },
    error: function(error) {
      $("#weather").html('<p>'+error+'</p>');
    }
  });
});

1 个答案:

答案 0 :(得分:0)

徽标包含在CodePen上的代码示例中使用的CSS中。 如果您的CSS中引用了HTML,则只需添加以下内容:

html += '<h2><i class="icon-'+weather.forecast[i].code+'"></i></h2>'

到你的for循环。

所以你的代码如下所示:

//My add here
for(var i=0;i<weather.forecast.length;i++) {
  html += '<p class="days">'+'<span class="d">'+weather.forecast[i].day+'</span>'+'<span class="val">'+weather.forecast[i].high+'</span>'+'</p>';
  html += '<h2><i class="icon-'+weather.forecast[i].code+'"></i></h2>';
}