1. 程式人生 > >用JS通過新浪天氣API介面獲取天氣

用JS通過新浪天氣API介面獲取天氣

先上程式碼


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="">
    <script type="text/javascript" src="js/jquery.js"></script>
</head
>
<body> <div id="w"></div> <script> $.getScript('http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city=&dfc=1&charset=utf-8',function(a){ var s="",r="",q="";for(s in window.SWther.w){ q=SWther.w[s][0]; r={city:s,date:SWther.add.now.split(" "
)[0]||"",day_weather:q.s1,night_weather:q.s2,day_temp:q.t1,night_temp:q.t2,day_wind:q.p1,night_wind:q.p2}, $("#w").html(q.s1+" "+r.city); //alert(q[0]) } });
</script> </body> </html>

獲取天氣的.
這裡首先是city= 是自動定位到你的城市的.

然後遍歷陣列,得到 city: 城市,day_weather:白天天氣,night_weather:夜晚天氣,day_temp:白天天氣,night_temp:夜晚天氣,day_wind:白天風,night_wind:夜晚風.

也可以在控制檯輸出一下console.log(window.SWther.w);