你的位置:首页 > Java教程

[Java教程]js获取新浪天气接口


 1 <!doctype html> 2 <html class="no-js fixed-layout"> 3 <head> 4  <meta charset="utf-8"> 5  <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6  <title>天气</title> 7 </head> 8 <body> 9 <div class="top-tool">10   <span id="sj"></span><span id="weather"><img width="16px" height="16px" src=""></span>11    <form action="/Search" method="get" id="qform" class="search-form" style="text-align:right;display:inline;float:right;padding-right:10px;" target="_blank">12     <span class="round">13     <input name="keywords" id="q" tabindex="1" class="input-key search-q ac_input" value="" maxlength="100" autocomplete="off">14     </span> <span class="button">15     <input type="submit" tabindex="2" id="su" value="搜&nbsp;&nbsp;索" class="ui-btn" style="padding: 0px 5px;">16     </span> 17    </form>18 </div>19 <script type="text/javascript" src="jquery-1.8.3.min.js"></script>20 <script type="text/javascript">21     function showLocale(objD) {22       var str, colorhead, colorfoot;23       var yy = objD.getYear();24       if (yy < 1900) yy = yy + 1900;25       var MM = objD.getMonth() + 1;26       if (MM < 10) MM = '0' + MM;27       var dd = objD.getDate();28       if (dd < 10) dd = '0' + dd;29       var hh = objD.getHours();30       if (hh < 10) hh = '0' + hh;31       var mm = objD.getMinutes();32       if (mm < 10) mm = '0' + mm;33       var ss = objD.getSeconds();34       if (ss < 10) ss = '0' + ss;35       var ww = objD.getDay();36       if (ww == 0) colorhead = "";37       if (ww > 0 && ww < 6) colorhead = "";38       if (ww == 6) colorhead = "";39       if (ww == 0) ww = "星期日";40       if (ww == 1) ww = "星期一";41       if (ww == 2) ww = "星期二";42       if (ww == 3) ww = "星期三";43       if (ww == 4) ww = "星期四";44       if (ww == 5) ww = "星期五";45       if (ww == 6) ww = "星期六";46       colorfoot = ""47       str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + ww + colorfoot;48       return (str);49     }50     function tick() {51       var today;52       today = new Date();53       document.getElementById("sj").innerHTML = showLocale(today);54       window.setTimeout("tick()", 1000);55     }56     tick();57 58 59     function findWeather() {60       var cityUrl = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';61       $.getScript(cityUrl, function (script, textStatus, jqXHR) {62         var citytq = remote_ip_info.city; // 获取城市63 64         citytq = "郑州";65         var url = "http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&city=" + citytq + "&day=0&dfc=3";66         $.ajax({67           url: url,68           dataType: "script",69           scriptCharset: "gbk",70           success: function (data) {71             var _w = window.SWther.w[citytq][0];72             var _f = _w.f1 + "_0.png";73             if (new Date().getHours() > 17) {74               _f = _w.f2 + "_1.png";75             }76             var img = "<img width='16px' height='16px' src='http://i2.sinaimg.cn/dy/main/weather/weatherplugin/wthIco/20_20/" + _f77         + "' />";78             var tq = "今日天气 : " + citytq + " " + img + " " + _w.s1 + " " + _w.t1 + "℃~" + _w.t2 + "℃ " + _w.d1 + _w.p1 + "级";79             $('#weather').html(tq);80           }81         });82       });83     }84     findWeather()85   </script>86 </body>87 </html>