您好,登錄后才能下訂單哦!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#result{width: 800px;border:1px solid #ddd;margin-top: 30px;background: #aaa;display: none;}
#result p{height: 25px;line-height: 25px;padding-left: 20px;color: #fff;font-weight: bold;font-size: 16px;}
#result p span{padding: 0 10px;display: inline-block;width: 120px;text-align:center;height: 25px;line-height: 25px;}
#result p span.temp{width: 250px;text-align: left;margin-left: 20px;}
</style>
<script src="Jquery/jquery-1.12.4.js"></script>
<script>
$(function(){
$(window).on('load',function(){
$.ajax({
'type':'get',
'url':'http://apis.baidu.com/heweather/weather/free?city=beijing',
'headers':{'apikey':'2c6d2927ef76200697018e989f6afbbb'},
'success':function(data){
$('#result').show();
data = data['HeWeather data service 3.0'][0];
var basic_info = '城市:' + data.basic.cnty + ' ' + data.basic.city;
var tianqi = data.daily_forecast;
$('<p><strong>' + basic_info + '</strong></p>').appendTo('#result');
$('<p><span>日期</span><span>氣溫</span><span class="temp">天氣</span></p>').appendTo('#result');
$.each(tianqi,function(index,element){
var html = '';
var date = element.date;
var temperature = element.tmp.min + '°C - ' + element.tmp.max + '°C';
var w1 = element.cond.txt_d;
var w2 = element.cond.txt_n;
var weather;
if(w1 == w2){
weather = w1;
}
else{
weather = w1 + ' 轉 ' + w2;
}
html += '<span>'+ date +'</span>' + '<span>'+ temperature +'</span>' + '<span class="temp">'+ weather +'</span>';
$('<p>'+ html +'</p>').appendTo('#result');
});
}
});
});
$('#query').on('click',function(){
$('#result').hide().empty();
var city = $.trim($('#city').val());
$.ajax({
'type':'get',
'url':'http://apis.baidu.com/heweather/weather/free',
'data':{'city':city},
'headers':{'apikey':'2c6d2927ef76200697018e989f6afbbb'},
'success':function(data){
$('#city').val('');
data = data['HeWeather data service 3.0'][0];
console.log(data);
var basic_info = '城市:' + data.basic.cnty + ' ' + data.basic.city;
var tianqi = data.daily_forecast;
$('<p><strong>' + basic_info + '</strong></p>').appendTo('#result');
$('<p><span>日期</span><span>氣溫</span><span>天氣</span></p>').appendTo('#result');
$.each(tianqi,function(index,element){
var html = '';
var date = element.date;
var temperature = element.tmp.min + '℃ - ' + element.tmp.max + '℃';
var w1 = element.cond.txt_d;
var w2 = element.cond.txt_n;
var weather;
if(w1 == w2){
weather = w1;
}
else{
weather = w1 + ' 轉 ' + w2;
}
html += '<span>'+ date +'</span>' + '<span>'+ temperature +'</span>' + '<span>'+ weather +'</span>';
$('<p>'+ html +'</p>').appendTo('#result');
$('#result').show();
});
}
});
});
});
</script>
</head>
<body>
<input type="text" placeholder="請輸入城市" id="city" />
<input type="button" value="查詢" id="query" />
<div id="result"></div>
</body>
</html>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。