<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js weather 天气查询</title> <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script> <script src="lib/jquery/jquery-3.2.1.js"></script> </head> <body> <script> /*参考:https://www.cnblogs.com/hellowoeld/p/10687624.html*/ function getTemperature(temperatureTxt) { /*根据温度文本信息,如:"高温 27℃","低温 17℃",获取温度,如:27℃, 17℃。*/ return temperatureTxt.split(' ')[1] } function getWeather() { /*获取今天的天气信息。*/ jQuery.support.cors = true; $.ajax({ url: "http://wthrcdn.etouch.cn/weather_mini", type: "GET", dataType: 'json', data: {city: '天津'}, // 所在的市 success: function (res) { console.log("res:", res); let city = res.data.city; // 所在城市 let curTemperature = res.data.wendu + '℃'; let highTemperatureTxt = res.data.forecast[0].high; // 最高温度 let lowTemperatureTxt = res.data.forecast[0].low; // 最低温度 let temperatureRange = getTemperature(lowTemperatureTxt) + '~' + getTemperature(highTemperatureTxt); let weatherType = res.data.forecast[0].type; // 天气状态 console.log(city, curTemperature, temperatureRange, weatherType); // 天津 25℃ 17℃~27℃ 阴 let fengxiang = res.data.forecast[0].fengxiang; let fengli = res.data.forecast[0].fengli.match(/\d+级/g)[0]; console.log(fengxiang, fengli); // 南风 2级 }, error: function (err) { console.log(err) } }); } getWeather(); </script> </body> </html>
11-28
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交