网页天气模块,包括当天天气和未来四天预报

摘要:
已改用和风天气API解决了该问题。自己制作了一个简单的天气模块,可以显示当天天气和未来四天的预报。效果图如下:HTML如下:-  星期  年月日选择城市:更新~--实时空气质量:-更新中……

已知问题:该API本地可以正常使用,如果在https页面下会由于ajax请求http的资源导致被block掉。已改用和风天气API解决了该问题。

自己制作了一个简单的天气模块,可以显示当天天气和未来四天的预报。效果图如下:

网页天气模块,包括当天天气和未来四天预报第1张

HTML如下:

<body>
    <div class="weather-box">
        <h5 class="today"><span class="thiscity">-</span>&nbsp;&nbsp;星期<span class="day"></span>&nbsp;&nbsp;<span class="year"></span><span class="month"></span><span class="date"></span><span class="choosecity">选择城市:<input type="text"name="city"class="city"value="北京"maxlength="6"></span>
        <span class="update">更新</span></h5>
        <div class="todayinfo">
            <h1 class="temprange">~</h1>
            <p class="type">-</p>
            <p class="wind">-</p>
            <p>实时空气质量:<span class="aqi">-</span></p>
        </div>
        <div class="todayotherinfo">
            <p class="time">更新中……</p>
            <p>实时:<span class="nowtemp"></span>°C</p>
            <h4>感冒指数:</h4>
            <p class="coldinfo"></p>
        </div>
        <div class="future">
            <ul>
                <li class="future-item1">
                    <h6 class="date">-</h6>
                    <h3 class="temprange">~</h3>
                    <p class="type">-</p>
                    <p class="wind">-</p>
                </li>
                <li class="future-item2">
                    <h6 class="date">-</h6>
                    <h3 class="temprange">~</h3>
                    <p class="type">-</p>
                    <p class="wind">-</p>
                </li>
                <li class="future-item3">
                    <h6 class="date">-</h6>
                    <h3 class="temprange">~</h3>
                    <p class="type">-</p>
                    <p class="wind">-</p>
                </li>
                <li class="future-item4">
                    <h6 class="date">-</h6>
                    <h3 class="temprange">~</h3>
                    <p class="type">-</p>
                    <p class="wind">-</p>
                </li>
            </ul>
        </div>
    </div>
</body>

CSS就不贴了,JS代码如下(jQuery):

$(function(){
    (function(){
        //以下获得今天的日期与星期
        functionupdatedate(){
            var now=newDate();
            var day=now.getDay();
            var year=now.getFullYear();
            var month=now.getMonth();
            var date=now.getDate();
            var time=now.toLocaleTimeString();
            var day2week="";
            switch(day) {
                case 0:
                    day2week="日";
                    break;
                case 1:
                    day2week="一";
                    break;
                case 2:
                    day2week="二";
                    break;
                case 3:
                    day2week="三";
                    break;
                case 4:
                    day2week="四";
                    break;
                case 5:
                    day2week="五";
                    break;
                case 6:
                    day2week="六";
                    break;
            }
            $(".today .day").text(day2week);
            $(".today .year").text(year);
            $(".today .month").text(month+1);
            $(".today .date").text(date);
            $(".todayotherinfo .time").text("更新时间:"+time);
        }
        //取出字符串中数字的方法
        String.prototype.str2num=function(){
            var reg=/[d-]/g;
            return parseInt(this.match(reg).join(""));
        };
        //更新所有天气信息
        functionupdate(){
            var city=$(".city").val()||"北京";
            var url="http://wthrcdn.etouch.cn/weather_mini?city="+city;
            $.ajax({
                url: url,
                success:function(info){
                    var tempinfo=JSON.parse(info);
                    //如果成功取得天气信息
                    if(tempinfo.status==1000){
                        //更新时间
updatedate();
                        //显示选择的城市
                        $(".today .thiscity").text(city);
                        //实时温度和感冒指数
                        $(".todayotherinfo .nowtemp").text(tempinfo.data.wendu);
                        $(".todayotherinfo .coldinfo").text(tempinfo.data.ganmao);
                        //更新今日天气详细信息
                        var today=tempinfo.data.forecast[0];
                        var temprange=today.low.str2num()+"~"+today.high.str2num();
                        $(".todayinfo .temprange").text(temprange);
                        $(".todayinfo .type").text(today.type);
                        var wind=today.fengli+today.fengxiang;
                        $(".todayinfo .wind").text(wind);
                        $(".todayinfo .aqi").text(tempinfo.data.aqi);
                        //未来四天预报
                        $(".future li").each(function(index) {
                            var idx=index+1;
                            var future=tempinfo.data.forecast[idx];
                            var date=future.date;
                            var temprange=future.low.str2num()+"~"+future.high.str2num();
                            var type=future.type;
                            var wind=future.fengli+future.fengxiang;
                            $(this).find('.date').text(date);
                            $(this).find('.temprange').text(temprange);
                            $(this).find('.type').text(type);
                            $(this).find('.wind').text(wind);
                        });
                    }else{
                        //无法取得城市的天气信息
                        $(".today .thiscity").text("无效的城市");
                    }
                }
            });
        }
        update();
        //点击更新
        $(".today .update").click(function(event) {
            update();
        });
        //每小时自动更新
        var updatetimer=setInterval(function(){
            update();
        },3600000);
    })();
});

关键点在于天气API,有了这个API,其他都很简单了。

http://wthrcdn.etouch.cn/weather_mini?city=城市

代码在我的Github:https://github.com/zhangcuiZC/My-FreeCodeCamp/tree/master/weather

2017/07/07

可能由于和风天气API的变动,导致返回的数据中少了air字段,有需要的可以根据返回数据结构修改源码。

免责声明:文章转载自《网页天气模块,包括当天天气和未来四天预报》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇以Docker容器的形式运行GVM-11Linux网卡bond模式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Kafka 权限管理实战(最全整理)

JAAS文件定义了链接KafkaBroker所需的用户名和密码,以及用于在代理的各个节点之间进行通信的用户名和口令:用户名/口令:用于代理之间通信的用户名与口令。...

压倒程序员的最后一个面试题,iOS性能优化的面试题

以及如何进行性能优化?关于instruments网上有很多资料,作为一个合格iOS开发者,熟悉这个工具还是很有必要的。一个tableview维持一个队列的数据可重用的UITableViewCell对象。这一过程即为Blending。在运行中缩放图片是很耗费资源的,特别是UIImageView嵌套在UIScrollView中的情况下。...

图论介绍(Graph Theory)

G-v具有比G更多的连通分支,因此v被称为G的截断点G-e具有比G多的连通分支。定理:连通图G,其中e是桥e不属于G的任何环有顶点u,v,使得任何路径u-v都通过e连通图G;而w是存储在顶点u,v处的割点,使得任意路径u-v通过w定义:顶点之间的距离x-y:所有x-y路径的最小长度。...

Fiddler抓包7-post请求(json)(转载)

2.查看上图中的红色框:这里只支持application/x-www-form-urlencoded格式的body参数,即json格式。您需要检查JOSN列中的five和xml。1.如果遇到text/xml格式的正文,如下图所示...

Python-正则

,三:量词*重复0次或多次{0,}+重复一次或多次{1,}?重复0或1次{1,0}{n}重复n次{n}{n,}重复n次,或更多次{n,m}将n次重复到m次Escape:如果字符串中有特殊字符要匹配,请在常规字符和字符串前面添加r。如果特殊字符在字符组中,则它们是匹配的特殊字符,但为了记忆,匹配时会转义所有特殊字符。...

QT学习之如何在QToolBar中添加带图标的QToolButton并设置图标大小

在网上查到了三种方法,找到一种比较好理解的。图标存放位置可在工程文件夹里创建自命名的文件夹如"res",再在根目录下创建qrc文件,如图:然后我们需要对qrc文件进行编辑:res/1.pngres/2.pngres/3.pngres/4.pngres/5.pngres/6.pngres/7.png这里的"res"是自己命名的存放图标的目录。接着我们需要在项目...