Javascript Switch语句似乎没有通过
我有一段基于simplewather.js的代码:Javascript Switch语句似乎没有通过,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一段基于simplewather.js的代码: $(document).ready(function() { var conditionsImageClass; $.simpleWeather({ location: 'Leon, Spain', woeid: '', unit: 'f', success: function(weather) { switch(weather.code) {
$(document).ready(function() {
var conditionsImageClass;
$.simpleWeather({
location: 'Leon, Spain',
woeid: '',
unit: 'f',
success: function(weather) {
switch(weather.code) {
case 27:
conditionsImageClass = '.diw-clouds:before';
break;
case 28:
conditionsImageClass = '.diw-clouds-moon:before';
break;
default:
conditionsImageClass = '.test'
break;
}
alert(weather.code);
alert(conditionsImageClass);
var loc = '<h5>'+weather.city+', '+weather.region+'</h5>';
html = '<p>Today</p><p><strong>'+returnsDate()+'</strong></p>';
html += '<i class="' + conditionsImageClass + '"></i>';
html += '<h2><strong><i class="icon-'+weather.code+'"></i> '+weather.temp+'°'+weather.units.temp+'</strong></h2>';
html += '<h5 class="currently">'+weather.currently+'</h5>';
$("#location").html(loc);
$("#weather").html(html);
},
error: function(error) {
$("#weather").html('<p>'+error+'</p>');
}
});
});
$(文档).ready(函数(){
var conditionsImageClass;
$.SimpleWither({
地点:'西班牙里昂',
可悲的是:“,
单位:f,
成功:功能(天气){
开关(天气代码){
案例27:
conditionsImageClass='.diw云:在'';
打破
案例28:
conditionsImageClass='.diw云月亮:在'';
打破
违约:
conditionsImageClass='.test'
打破
}
警报(天气代码);
警报(conditionsImageClass);
var loc=''+天气.城市+'',''+天气.地区+'';
html='今天'+returnsDate()+'';
html+='';
html+=''+weather.temp+'°;'+weather.units.temp+'';
html+=''+天气。当前为+'';
$(“#位置”).html(loc);
$(“#weather”).html(html);
},
错误:函数(错误){
$(“#weather”).html(“”+错误+””);
}
});
});
现在switch语句似乎没有执行。我还没有完全充实它,因为我想在当前条件下测试它。现在,莱昂的状态代码是‘28’,或者大部分是阴天。但switch语句始终默认,即使有“28”的情况。你知道它为什么这么做吗?如果您需要更多代码,请告诉我
天气。code
可能是一个字符串。您的案例正在检查整数,因此它们不匹配。您需要使用parseInt()
解析天气代码,或者将每个大小写转换为字符串
switch(parseInt(weather.code)) {
case 27:
conditionsImageClass = '.diw-clouds:before';
break;
case 28:
conditionsImageClass = '.diw-clouds-moon:before';
break;
default:
conditionsImageClass = '.test';
}
显示差异的简化示例
console.log(displayWeatherCode(27));//"27!"
console.log(显示天气代码(“27”);//“默认!”
功能显示weatherCode(天气代码){
开关(天气代码){
案例27:
返回“27!”;
案例28:
返回“28!”;
违约:
返回“默认值!”;
}
}
天气。code
可能是一个字符串。您的案例正在检查整数,因此它们不匹配。您需要使用parseInt()
解析天气代码,或者将每个大小写转换为字符串
switch(parseInt(weather.code)) {
case 27:
conditionsImageClass = '.diw-clouds:before';
break;
case 28:
conditionsImageClass = '.diw-clouds-moon:before';
break;
default:
conditionsImageClass = '.test';
}
显示差异的简化示例
console.log(displayWeatherCode(27));//"27!"
console.log(显示天气代码(“27”);//“默认!”
功能显示weatherCode(天气代码){
开关(天气代码){
案例27:
返回“27!”;
案例28:
返回“28!”;
违约:
返回“默认值!”;
}
}
您不能同时使用位置:'Leon,Spain',+woeid:'',。
为了识别位置,您可以在案例位置中使用有效字段
$.simplewather({
地点:'西班牙里昂',
单位:f,
成功:功能(天气){
console.log('weather.code为:'+weather.code);
开关(天气代码){
案例‘27’:
conditionsImageClass='.diw云:在'';
打破
案件‘28’:
conditionsImageClass='.diw云月亮:在'';
打破
违约:
conditionsImageClass='.test'
打破
}
log('conditionsImageClass为:'+conditionsImageClass);
},
错误:函数(错误){
console.log('错误:'+错误);
}
});代码>
您不能同时使用位置:'Leon,Spain',+woeid:'',。
为了识别位置,您可以在案例位置中使用有效字段
$.simplewather({
地点:'西班牙里昂',
单位:f,
成功:功能(天气){
console.log('weather.code为:'+weather.code);
开关(天气代码){
案例‘27’:
conditionsImageClass='.diw云:在'';
打破
案件‘28’:
conditionsImageClass='.diw云月亮:在'';
打破
违约:
conditionsImageClass='.test'
打破
}
log('conditionsImageClass为:'+conditionsImageClass);
},
错误:函数(错误){
console.log('错误:'+错误);
}
});代码>
我在猜测天气。code正在返回字符串。您需要首先解析它console.log(weather)
至少……我猜weather.code正在返回一个字符串。您需要首先解析它console.log(weather)
至少……使用location,它实际上是如何获得位置的?@pmath325from:您只需提供您的美国邮政编码、WOEID、lat/long或世界上的任何位置。当我同时使用您的空WOEID参数进行测试时,我发现了一个错误。使用location,它实际上是如何获取位置的?@pmath325 From:您只需提供您的美国邮政编码、WOEID、lat/long或世界上的任何位置。当我同时使用您的空WOEID参数进行测试时,我得到了一个错误。