我有基于simpleweather.js的以下代码块:

$(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+'&deg;'+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>');
    }
  });
});


现在,switch语句似乎没有执行。我还没有完全充实它,因为我想在当前条件下对其进行测试。目前,莱昂的条件代码为“ 28”或大部分多云。但是,即使有'28'的情况,switch语句也总是默认的。有什么想法为什么要这么做吗?让我知道是否需要更多代码。

最佳答案

您不能同时使用位置:“西班牙里昂”,“ + woeid:”​​。
为了确定位置,您可以在案例位置中使用有效字段。



$.simpleWeather({
    location: 'Leon, Spain',
    unit: 'f',
    success: function(weather) {
        console.log('weather.code is: ' + weather.code);
        switch(weather.code) {
            case '27':
                conditionsImageClass = '.diw-clouds:before';
                break;
            case '28':
                conditionsImageClass = '.diw-clouds-moon:before';
                break;
            default:
                conditionsImageClass = '.test'
                break;
        }
        console.log('conditionsImageClass is: ' + conditionsImageClass);
    },
    error: function(error) {
        console.log('Error: ' + error);
    }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script>

10-05 20:53
查看更多