js获取用户当地的日期和天气信息
$.ajax({
url: 'http://pv.sohu.com/cityjson?ie=utf-8',
dataType: 'script',
async: false,
success: function () {
var city = returnCitySN.cname;
var a = city.indexOf('省', 0);
if (a >= 0) {
city = city.substring(a+1);
}
$.ajax({
url: "http://wthrcdn.etouch.cn/weather_mini?city="+city,
dataType: 'json',
async: false,
success: function (data) {
var todayWeather = data.data.forecast[0]; // 天气
var date = new Date() // 日期
var year = date.getFullYear()
var month = date.getMonth() + 1
$('#detail').html(`${year}年${month}月${todayWeather.date} ${city} ${todayWeather.type} ${todayWeather.high} ${todayWeather.low}`)
}
})
}
})
获得结果如下图:
获取到的天气信息还有风向、风力等等,根据自己需求展示。
做人嘛,最重要的是开心啦!der