创建一个对象,将每个天气图标的URL放入对象中,名称代表天气条件,然后使用正确的名称获取正确的URL:
var weatherIcons = {
snowy:"/snowyicon.jpg",
rainy:"/rainyicon.jpg",
sunny:"/sunnyicon.jpg"
};
...
var condition = parsed_json['current_observation']['weather'];
var iconTOUse = weatherIcons[condition];
var element = '<img src="'+iconTOUse+'" />';
在这里:
"<div class='icone'><img src='http://icons-ak.wxug.com/i/c/k/"+wicon+".gif' alt='"+condition+"' title='"+condition+"'/></div>"
"<div class='icone'><img src='http://YourServer.com/yourIcons/"+ wicon +".gif' alt='"+condition+"' title='"+condition+"'/></div>"
确保您的图标与他们相同。现在,如果您坚持使用不同的名称,则需要将其名称映射到您的名字中,这应该很琐碎。
const weather = response.weather;
if (weather === "Clear") {
weatherIcon.src = "sun.png";
} else if (weather === "Clouds") {
weatherIcon.src = "cloud.png";
} else if (weather === "Rain") {
weatherIcon.src = "rain.png";
} else {
weatherIcon.src = "default.png";
}
console.log(response);
})
.catch(err => console.error(err));