其他分享
首页 > 其他分享> > 获取天气api

获取天气api

作者:互联网

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }

            .show {
                width: 220px;
                height: auto;
                background-color: #FFC0CB;
                text-align: center;
            }
        </style>
        <script type="text/javascript">
            $(function() {
                let vals = $("input")[0]
                let btn = $("button")
                btn.click(function() {
                    $.ajax({
                        type: "GET",
                        url: 'http://wthrcdn.etouch.cn/weather_mini?city='+vals.value,
                        success(res) {
                            const data =  $.parseJSON(res); 
                            console.log(res)
                            let citys = data.data.city
                            let info = data.data.forecast[0];
                             document.querySelector(".city").innerHTML = "当前城市是"+citys
                             document.querySelector(".week").innerHTML = "4月"+info.date
                             document.querySelector(".high").innerHTML = info.high
                             document.querySelector(".low").innerHTML = info.low
                             document.querySelector(".fengxiang").innerHTML = "风向:"+info.fengxiang
                             document.querySelector(".type").innerHTML = info.type
    
                            /*
                                date: "7日星期四"
                                fengli: "<![CDATA[1级]]>"
                                fengxiang: "南风"
                                high: "高温 24℃"
                                low: "低温 9℃"
                                type: "晴"
                            */
                        }
                    })
                })
            })
        </script>
    </head>
    <body>
        <h3>天气</h3>
        <input type="text">
        <button>查询</button>
        <div class="show">
            <p class="city"></p>
            <p class="week"></p>
            <p class="high"></p>
            <p class="low"></p>
            <p class="fengxiang"></p>
            <p class="type"></p>
        </div>
    </body>
</html>

 

标签:info,type,天气,innerHTML,获取,api,querySelector,document,data
来源: https://www.cnblogs.com/happyTo/p/16113511.html