我正在使用openweathermap显示天气预报.一切正常,但图标有问题.
json响应代码是:
json响应代码是:
Array ( [city] => Array ( [id] => 1271476 [name] => Guwahati [coord] => Array ( [lon] => 91.751 [lat] => 26.1862 ) [country] => IN [population] => 899094 ) [cod] => 200 [message] => 0.0630711 [cnt] => 1 [list] => Array ( [0] => Array ( [dt] => 1495688400 [temp] => Array ( [day] => 33 [min] => 24.89 [max] => 33.82 [night] => 24.89 [eve] => 30.6 [morn] => 33 ) [pressure] => 1013.02 [humidity] => 90 [weather] => Array ( [0] => Array ( [id] => 500 [main] => Rain [description] => light rain [icon] => 10d ) ) [speed] => 3.92 [deg] => 88 [clouds] => 24 [rain] => 2.73 ) ) )
现在我该如何显示图标:[weather] [0] [icon] => 10D
什么是10d&我怎样才能得到图标的网址.
解决方法
好吧,我知道使用jQuery的方法.
<div id="icon"><img id="wicon" src="" alt="Weather icon"></div>
在上面的HTML中你看到缺少的唯一东西是src属性,所以让我们用一些jQuery和JavaScript填充它.
您可以创建一个变量来保存API提供的图标代码,如:
var iconcode = a.weather[0].icon;
在它之后你应该将这个var图标代码与包含图标的url连接起来,例如:
var iconurl = "http://openweathermap.org/img/w/" + iconcode + ".png";
最后,通过执行以下操作来更改DOM中的src属性:
$('#wicon').attr('src',iconurl);
我希望这能解决问题. 原文链接:https://www.f2er.com/html/225203.html