首页 > Web开发 > 详细

利用中国天气预报接口实现简单天气预报

时间:2016-01-31 14:51:53      阅读:150      评论:0      收藏:0      [点我收藏+]

复制代码 代码如下:

<?php
header("content-type:text/html;charset=utf-8");
$weather = file_get_contents("http://www.weather.com.cn/data/sk/101280601.html");
echo $weather;
?>

复制代码 代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gbk" />
<style type="text/css">
.all span {font:bold 30px/50px "宋体";color:red;}
</style>
<title>天气预报</title>
</head>
<body>
<div class="all">
这里是:<span class="place">城市</span>,
气温是<span class="temp">气温</span>,
风向:<span class="wind">风向</span>,
风力:<span class="windPower">风力</span>
</div>
<script type="text/javascript" src="http://127.0.0.1/jquery.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
//请求的地址
url : "http://127.0.0.1/weather.php",
//请求成功后执行的函数
success : function (data) {
//用eval()解析返回来的数据,将字符串转成JSON格式
var oD = eval(‘(‘+data+‘)‘);
//用jquery-1.8.2获取元素
var $place = $(".place"),
$temp = $(".temp"),
$wind = $(".wind"),
$windPower = $(".windPower");
//将返回来的数据放到相应的位置
$place.html(oD["weatherinfo"]["city"]);
$temp.html(oD["weatherinfo"]["temp"] + "°");
$wind.html(oD["weatherinfo"]["WD"]);
$windPower.html(oD["weatherinfo"]["WS"]);
}
});
})
</script>
</body>
</html>

利用中国天气预报接口实现简单天气预报

原文:http://www.jb51.net/article/45950.htm

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!