【发布时间】:2013-04-19 17:25:48
【问题描述】:
所以,我正在构建一个天气网站,并使用 Wunderground 的 API。在 AJAX XML 转储中,“icon”的值似乎是要走的路。此响应将动态更改页面上的 CSS。我有多个我想要的页面状态,但我似乎无法让它们工作。它似乎在第一个“if”语句之后停止。我需要它来检查是否 wu_icon == 某些条件,如果不是,则移动到下一个并查看 wu_icon == 下一组条件。
这是website。
任何帮助将不胜感激。
function get_weather(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
wu_query = latitude + ',' + longitude;
//Weather underground request URL format.
wu_url = wu_base_url + '/' + wu_api_key + '/' + wu_features + '/q/' + wu_query + '.json';
console.log('URL Query: ' + wu_url);
$.ajax({
url: wu_url,
dataType: "jsonp",
success: function(parsed_json) {
console.log(parsed_json);
var location = parsed_json['location']['city'];
var wuurl = parsed_json['location']['wuiurl'];
//simple forecast day 1.
var wu = parsed_json['current_observation'];
var wu_temp_f = wu['temp_f'];
var wu_high_temp = wu['high'];
var wu_low_temp = wu['low'];
var wu_icon = wu['icon'];
$('#location').html(location);
$('#temp').html(wu_temp_f + '°F');
if (wu_icon == 'partlycloudy' || 'partlysunny' || 'unkown'){
document.getElementById('rainclouds').style.display = 'none';
document.getElementById('topclouds').style.display = 'block';
document.getElementById('bottomclouds').style.display = 'block';
document.getElementById('canvas').style.display = 'none';
document.getElementById('snow').style.display = 'none';
window.alert("1");
}
else if (wu_icon == 'rain' || 'chancerain' || 'chancetstorms' || 'tstorms'){
document.getElementById('rainclouds').style.display = 'block';
document.getElementById('topclouds').style.display = 'none';
document.getElementById('bottomclouds').style.display = 'none';
document.getElementById('canvas').style.display = 'block';
document.getElementById('snow').style.display = 'none';
window.alert("2");
}
else if (wu_icon == 'flurries' || 'snow' || 'chanceflurries' || 'chancesnow'){
document.getElementById('rainclouds').style.display = 'block';
document.getElementById('topclouds').style.display = 'none';
document.getElementById('bottomclouds').style.display = 'none';
document.getElementById('canvas').style.display = 'none';
document.getElementById('snow').style.display = 'block';
window.alert("3");
}
else if (wu_icon == 'clear' || 'mostlysunny' || 'sunny'){
document.getElementById('rainclouds').style.display = 'none';
document.getElementById('topclouds').style.display = 'block';
document.getElementById('bottomclouds').style.display = 'none';
document.getElementById('canvas').style.display = 'none';
document.getElementById('snow').style.display = 'none';
window.alert("4")
}
else if (wu_icon == 'chancesleet' || 'sleet'){
document.getElementById('rainclouds').style.display = 'block';
document.getElementById('topclouds').style.display = 'none';
document.getElementById('bottomclouds').style.display = 'none';
document.getElementById('canvas').style.display = 'block';
document.getElementById('snow').style.display = 'block';
window.alert("5");
}
else if (wu_icon == 'cloudy' || 'mostlycloudy' || 'fog' || 'hazy'){
document.getElementById('rainclouds').style.display = 'block';
document.getElementById('topclouds').style.display = 'none';
document.getElementById('bottomclouds').style.display = 'none';
document.getElementById('canvas').style.display = 'none';
document.getElementById('snow').style.display = 'none';
window.alert("6");
}
}
【问题讨论】:
-
如果第一个 if 语句为真,则不会检查其他语句,因为您使用的是 if else
-
对,但是我要拉的位置正在返回字符串 'clear',所以它应该一直运行到它到达第四个 else if 语句,但事实并非如此。
-
PS:我正在使用警报来查看它的位置。
-
谢谢,这似乎已经解决了这个问题!这是我第一次在 if 语句中处理多个条件,所以谢谢!希望您当地的天气现在是准确的!
-
是的,这里的 atm 天气确实很糟糕;)很高兴我能提供帮助,请接受答案作为解决方案。
标签: javascript if-statement weather wunderground