聚合數據天氣預報API:https://www.juhe.cn/docs/api/id/39
HTML部分代碼:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" >
<title>天氣預報</title>
<script src="jquery-2.1.1.min.js"></script>
<link rel="stylesheet" href="w.css">
</head>
<body>
<div id="mf_weather">
<script src="w.js"></script>
<button id="search">天氣查詢</button>
<input id="city" type="text" value="tbody">
<div class="ctn">
<div id="mufeng">
</div>
<div id="future"></div>
</div>
</div>
</body>
</html>
JavaScript部分:
$(function(){
/*
* 1.輸入城市名
* 2,點擊的時候發送請求
* 3.響應成功渲染頁面
* */
$('#search').on('click',function(){
var city = $('#city').val()||'北京';
$citycode=urlencode(city);
url='http://v.juhe.cn/weather/index?format=2&cityname='+$citycode+'&key=c82727e986a4f6cfc6ba1984f1f9183a';
$.ajax({url: url,
dataType: "jsonp",
type:"get",
data:{location:city},
success:function(data){
var sk = data.result.sk;
var today = data.result.today;
var futur = data.result.future;
var fut = "日期溫度天氣風向";
$('#mufeng').html("<p>" + '當前: ' + sk.temp + '℃ , ' + sk.wind_direction + sk.wind_strength + ' , ' + '空氣溼度' + sk.humidity + ' , 更新時間' + sk.time + "</p><p style='padding-bottom: 10px;'>" + today.city + ' 今天是: ' + today.date_y + ' ' + today.week + ' , ' + today.temperature + ' , ' + today.weather + ' , ' + today.wind + "<p></p>");
$('#future').html("<p>" + '未來: ' + futur[0].temperature+ '℃ , ' + futur[0].weather + futur[0].wind + ' , ' + ' , 更新時間' + futur[0].week+futur[0].date + "</p><p style='padding-bottom: 10px;'>" + today.city + "<p></p>");
} });
});
function urlencode (str) {
str = (str + '').toString();
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
})
預覽圖(比較簡單粗糙)
文章來源:https://my.oschina.net/mfeng/blog/757756