实验三:输入城市,查找该城市的天气
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
document.addEventListener('plusready', function(){
//console.log("所有plus api都应该在此事件发生后调用,否则会出现plus is undefined。")
});
/*----------Jquery事件--------------*/
$(document).ready(function(){
$("button").click(function(){
/*-----------------------------------begin--------------------------------*/
/*------说明:通过ajax获取远程服务器(url:http://wthrcdn.etouch.cn/weather_mini?city=)天气信息(json数据),解析获取的字条串,
转化成为对象,获取forecast数组,调用addBox()函数---------*/
var city = $("#city").val();
$.ajax({
type:"GET",url:"http://wthrcdn.etouch.cn/weather_mini?city="+city,
dataType:"json",
success:function(result){
addBox(result.data.forecast);
}
});
});
});
function addBox(json_data) {
$.each(json_data, function(index, obj) {
$("#box").append("<div'>" +
"<p>" + obj['date'] + "</p>" +
"<p>" + obj['high'] + "</p>" +
"<p>" + obj['fengli'] + "</p>" +
"<p>" + obj['low'] + "</p>" +
"<p>" + obj['fengxiang'] + "</p>" +
"<p>" + obj['type'] + "</p>" +
"</div>");
});
}
</script>
</head>
<body>
<div>
<h3>请输入城市:</h3>
城市:<input type="text" id="city"/>
<br>
<button type="button">提交</button>
</div>
<br>
<div id="box"></div>
</body>
</html>
浏览器显示如下:
输入扬州,点击提交: