随笔 - 50  文章 - 9  评论 - 44  阅读 - 27718

免费天气预报插件jquery版本

天气信息来源:中国天气网【weather.com.cn】
根据QQ[http://fw.qq.com:80/ipaddress]得到ip地址和所在城市信息,
然后调用weather.com.cn网站的xml得到城市代码,就可以得到JSON格式的天气信息
setWeather函数可以自行修改生成您想要的天气信息格式

有些兄弟说不好用可能是没有仔细看,我重新整理一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<script language="javascript" src="jquery.js"></script>
<script language='javascript' src='jquery.cookie.js'></script>
<script language='javascript' src='weather.js'></script>
<div align="center">
    <span id='jsCity'> </span>
    <span id='jsWeather'> </span>
</div>
</body>
</html>



Weather.js文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
'**********************************************
' 文件名: 免费天气插件jquery版本
' 日期:  2010年07月01日
' 声明: 
'   本代码可以自由使用,但请保留此版权声明信息
'   如果您对本代码进行修改增强,
'   请发送一份给俺。
'**********************************************
*/
 
//jsCity、jsWeather为您html中的元素ID
function setWeather(tID,tCity,tTip,tWendu,tFengli)
{
 $("#jsCity").html("<a href='http://www.weather.com.cn/html/weather/"+tID+".shtml' target=_bank>"+tCity+"</a>");
 $("#jsWeather").html(tTip+" "+tWendu);
}
 
var cityid,city1,city2,weaXML,weaHTML
weaXML = "http://service.weather.com.cn/plugin/"
weaHTML = "http://m.weather.com.cn/data/"
$().ready(function() {
 cityid = $.cookie('wea_cityid');
 if(cityid==null){
  LoadJS("http://fw.qq.com:80/ipaddress", function()
  {
   if (typeof IPData != "undefined")
   {
    city1 = IPData[2];
    city2 = IPData[3];
    city1 = city1.replace("省","")
    city1 = city1.replace("市","");
    if(city2=="") city2 = city1;
    $.cookie('wea_cityip', IPData[0]);
    getLocalCity("data/city.xml",0);
   }
  });
 }
 else{
  getWeather(cityid);
 }
});
function getLocalCity(turl,b)
{
 $.ajax({
    type: "GET",
    url: weaXML+turl,
    dataType: "text",
    success: function(msg){
   var cityArr = msg.split(",");
   for(var i=0;i <cityArr.length;i++){
    cid = cityArr[i].split("|")[0];
    if(b<3){
     if(cityArr[i].split("|")[1]==city1 || cityArr[i].split("|")[1]==city2){
      getLocalCity("data/city"+ cid +".xml",b+1);
      break;
     }
    }
    else{
     cid = cityArr[i].split("|")[1];
     getWeather(cid);
     break;
    }
   }
    }
 });
}
function getWeather(cid)
{
 $.cookie('wea_cityid', cid,{expires: 365});
 var weajs = weaHTML+cid+'.html';
 $.getJSON(
  weajs,
  function(objJson){
   var cityname=objJson.weatherinfo.city;  //上海
   var id=objJson.weatherinfo.cityid;   //101020100
   var cityinfo1=objJson.weatherinfo.weather1; //晴转多云
   var cityinfo2=objJson.weatherinfo.weather2;
   var wd1=objJson.weatherinfo.wind1;   //北风3-4级
   var wd2=objJson.weatherinfo.wind2;
   var fl1=objJson.weatherinfo.fl1;   //3-4级
   var fl2=objJson.weatherinfo.fl2;
   var temp1=objJson.weatherinfo.temp1;  //4℃~-1℃
   var temp2=objJson.weatherinfo.temp2;
   var img1=objJson.weatherinfo.img1;
   var img2=objJson.weatherinfo.img2;
   var img3=objJson.weatherinfo.img3;
   var img4=objJson.weatherinfo.img4;
   var index=objJson.weatherinfo.index;
   var index_d=objJson.weatherinfo.index_d;
   var index_xc=objJson.weatherinfo.index_xc;
   var index_uv=objJson.weatherinfo.index_uv;
   var date=objJson.weatherinfo.date;
   var date_y=objJson.weatherinfo.date_y;
   var imgtitle1=objJson.weatherinfo.img_title1;
   var imgtitle2=objJson.weatherinfo.img_title2;
   var imgsingle=objJson.weatherinfo.img_single;
   var imgtitlesingle=objJson.weatherinfo.img_title_single;
    
   setWeather(id,cityname,cityinfo1,temp1,wd1)
 });
}
function LoadJS(jsUrl,fCallBack)
{
 var _script = document.createElement('script');
 _script.setAttribute('type', 'text/javascript');
 _script.setAttribute('charset', 'gb2312');
 _script.setAttribute('src', jsUrl);
 document.getElementsByTagName('head')[0].appendChild(_script);
 if(typeof fCallBack != "undefined")
 {
  if ($.browser.msie)
  {
   _script.onreadystatechange = function()
   {
    if (this.readyState=='loaded' || this.readyState=='complete')
    {fCallBack();}
   };
  }
  else if ($.browser.mozilla)
  {
   _script.onload = function(){fCallBack();};
  }
  else
  {
   fCallBack();
  }
 }
}
最后注意事项:需要用到jquery cookie的插件,此插件很多地方有用到。
1
Author:winbeanye
1
Date:2010-07-09
1
MyWebSite:<a href="http://www.5ujd.net" rel="noopener nofollow">http://www.5ujd.net</a><br>
posted on   脑瓜子  阅读(2336)  评论(3编辑  收藏  举报

点击右上角即可分享
微信分享提示