[每天进步一点点~] js、jquery调用接口获取天气

天气预览图
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>天气</title>
 <style>
        .weather-list {
            width: 100%;
            /*margin-top: 15px;*/
            /*font-size: 12px;*/
            background-color: pink;
        }
        .weather-list ul {
            width: 20%;
            float: left;
            background-color: pink;
        }
        .weather-list ul li {
            text-align: center;
            color: white;
            width: 160px;
            overflow: hidden;
        }
        .weather-list ul li.text {
            height: 20px;
        }
        .weather-list img {
            width: 50px;
            height: 50px;
        }
    </style>
</head>
<body>

<div id="wea"></div >
<div class="weather-list"></div>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    $.ajax({
        type: 'GET',
        url: 'http://wthrcdn.etouch.cn/weather_mini?city=北京',
        dataType: 'JSON',
        error: function () {
            alert('网络错误');
        },
        success: function (res) {
            console.log(res);
            $('#wea').append(res.data.city);
        }
    });


    getWeather();
    function getWeather()
    {
        $.getJSON('http://wthrcdn.etouch.cn/weather_mini?city=北京', function (result) {
            var update_time = Date.parse(result.data.forecast[0].date);
            update_time = getTime(update_time);
            $('.tianqi_icon').html(setWeatherIcon(result.data.forecast[0].type));

            $('.weather-header-city').append(result.city);
            $('.weather-content-content .wea').html(result.data.forecast[0].type);
            $('.weather-content-content .tem').html(result.data.forecast[0].high + '~' + result.data.forecast[0].low);

            $('.weather-list').html('');
            var str = '';
            var tianqi_icon = setWeatherIcon(result.data.forecast[0].type);
            var wea = result.data.forecast[0].type;
            var tem = result.data.forecast[0].high + '~' + result.data.forecast[0].low;
            var week = '今天';
            for (var i = 0; i < 3; i++)
            {
                if (i > 0) {
                    tianqi_icon = setWeatherIcon(result.data.forecast[i].type);
                    week = result.data.forecast[i].date;
                    wea = result.data.forecast[i].type;
                    tem = result.data.forecast[i].high + '~' + result.data.forecast[i].low;
                }
                str +='<ul>\n' +
                    '            <li>'+week+'</li>\n' +
                    '            <li>'+tianqi_icon+'</li>\n' +
                    '            <li class="text">'+wea+'</li>\n' +
                    '            <li>'+tem+'</li>\n' +
                    '        </ul>';
            }
            $('.weather-list').html(str);
        })
    }

    // 根据天气设置背景图片
    function setBgImg(wea)
    {
        var bg_img = '';
        if (wea.indexOf("阴") >= 0) {
            bg_img = 'url("images/duoyun.png")';
        } else if (wea.indexOf("小雨") >= 0) {
            bg_img = 'url("images/xiaoyu.png")';
        } else if (wea.indexOf("多云") >= 0) {
            if (wea.indexOf("小雨") >= 0) {
                bg_img = 'url("images/xiaoyu.png")';
            } else {
                bg_img = 'url("images/duoyun.png")';
            }
        } else {
            bg_img = 'url("images/qingtian.png")';
        }
        return bg_img;
    }

    // 根据天气设置图标
    function setWeatherIcon(wea)
    {
        var wea_img = '';
        if (wea.indexOf("阴") >= 0) {
            wea_img = '<img src="images/duoyun.png" />';
        } else if (wea.indexOf("小雨") >= 0) {
            wea_img = '<img src="images/xiaoyu.png" />';
        } else {
            wea_img = '<img src="./images/qingtian.png" />';
        }
        return wea_img;
    }

    function getTime(timestamp)
    {
        var date = new Date(timestamp);
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes());
        return h+m;
    }

</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值