转载地址: http://www.cnblogs.com/blue-lg/archive/2012/03/04/2379564.html
html:
View Code
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>js版天气预报</title>
<link href="prviate.css" rel="stylesheet" type="text/css">
<script src="jquery.js"></script>
<script src="weather.js" language="javascript" type="text/javascript" charset="UTF-8"></script>
</head>
<body>
<div class="clear_36"></div>
<form action="#" onSubmit="return false;">
<dl class="weather_case">
<dt class="weather_search_def"><a href="http://daluzai.net/"><img src="./skin/dalz.jpg"></a></dt>
<dd class="weather_search_v form_val" id="weather_search"><input name="city" type="text" />
</dd>
<dd class="common_front weather_conn form_sub" id="weather_sub">
<input name="call" type="button" value="查询" title="查询天气预报" />
</dd>
<dd class="weather_author" id="weather_author"><p>查看源码</p></dd>
<p style='color:#f0f'>天气查询小帮手</p>
</dl>
</form>
<div class="clear_13"></div>
<div class="weather_res_case common_hide" id="weather_res_case">
<dl class="weather_res_item weather_res_stoday">
<dd><b id="city"></b></dd>
<dd>实时气温:<span id="temperature"></span></dd>
<dd>天气情况:<span id="weather"></span></dd>
<dd>风向:<span id="direction"></span></dd>
<dd>风力情况:<span id="wind"></span></dd>
</dl>
<dl class="weather_res_item weather_res_today">
<dd class="weather_res_dis_img" id="T_weather_img"></dd>
<dd>
<h2>今天</h2>
</dd>
<dd>天气情况:<span id="T_weather"></span></dd>
<dd>气温:<span id="T_temperature"></span></dd>
<dd>风向:<span id="T_direction"></span></dd>
</dl>
<dl class="weather_res_item weather_res_tmw">
<dd class="weather_res_dis_img" id="M_weather_img"></dd>
<dd>
<h2>明天</h2>
</dd>
<dd>天气情况:<span id="M_weather"></span></dd>
<dd>气温:<span id="M_temperature"></span></dd>
<dd>风向:<span id="M_direction"></span></dd>
</dl>
<dl class="weather_res_item weather_res_aftmw">
<dd class="weather_res_dis_img" id="L_weather_img"></dd>
<dd>
<h2>后天</h2>
</dd>
<dd>天气情况:<span id="L_weather"></span></dd>
<dd>气温:<span id="L_temperature"></span></dd>
<dd>风向:<span id="L_direction"></span></dd>
</dl>
<div class="clear_debug"></div>
</div>
<div class="clear_13"></div>
<script type="text/javascript">
//创建天气预报核心对象
var weather_=new weather();
//输入框的默认提示
def_value('weather_search','请输入要查询的城市天气');
//监听用户事件
onEvent.onEvent();
</script>
</body>
</html>
weather.js :
View Code
var weather=function(){
var tmp=0;
style='qq';
var SWther={};
this.getWeather=function(city){//根据city的值获取天气信息
$.getScript("http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=2&city="+city+"&dfc=3",function(){echo(city);});
}
function dis_img(weather){//显示不同天气对应的图片
var route="./skin/weather/";//文件夹路径
if(style=='unknow'){var forder='unknow';}
else{var forder='qq';}
var style_img=route+forder+"/s_13.png";
if(weather.indexOf("多云")!==-1||weather.indexOf("晴")!==-1){//多云转晴,以下类同 indexOf:包含字串
style_img=route+forder+"/s_1.png";}
else if(weather.indexOf("多云")!==-1&&weather.indexOf("阴")!==-1){
style_img=route+forder+"/s_2.png";}
else if(weather.indexOf("阴")!==-1&&weather.indexOf("雨")!==-1){
style_img=route+forder+"/s_3.png";}
else if(weather.indexOf("晴")!==-1&&weather.indexOf("雨")!==-1){
style_img=route+forder+"/s_12.png";}
else if(weather.indexOf("晴")!==-1&&weather.indexOf("雾")!==-1){
style_img=route+forder+"/s_12.png";}
else if(weather.indexOf("晴")!==-1){style_img=route+forder+"/s_13.png";}
else if(weather.indexOf("多云")!==-1){style_img=route+forder+"/s_2.png";}
else if(weather.indexOf("阵雨")!==-1){style_img=route+forder+"/s_3.png";}
else if(weather.indexOf("小雨")!==-1){style_img=route+forder+"/s_3.png";}
else if(weather.indexOf("中雨")!==-1){style_img=route+forder+"/s_4.png";}
else if(weather.indexOf("大雨")!==-1){style_img=route+forder+"/s_5.png";}
else if(weather.indexOf("暴雨")!==-1){style_img=route+forder+"/s_5.png";}
else if(weather.indexOf("冰雹")!==-1){style_img=route+forder+"/s_6.png";}
else if(weather.indexOf("雷阵雨")!==-1){style_img=route+forder+"/s_7.png";}
else if(weather.indexOf("小雪")!==-1){style_img=route+forder+"/s_8.png";}
else if(weather.indexOf("中雪")!==-1){style_img=route+forder+"/s_9.png";}
else if(weather.indexOf("大雪")!==-1){style_img=route+forder+"/s_10.png";}
else if(weather.indexOf("暴雪")!==-1){style_img=route+forder+"/s_10.png";}
else if(weather.indexOf("扬沙")!==-1){style_img=route+forder+"/s_11.png";}
else if(weather.indexOf("沙尘")!==-1){style_img=route+forder+"/s_11.png";}
else if(weather.indexOf("雾")!==-1){style_img=route+forder+"/s_12.png";}
else{style_img=route+forder+"/s_2.png";}
return style_img;
}
function echo(city){
//console.log(window);
//console.log(window.SWther.w[city]);
if(window.SWther.w[city]===undefined){alert('请输入一个正确的城市名称');return false;}
uisy.query('return');
$('#city').html(city);
$('#weather').html(window.SWther.w[city][0].s1);
$('#temperature').html(window.SWther.w[city][0].t1+'°');
$('#wind').html(window.SWther.w[city][0].p1);
$('#direction').html(window.SWther.w[city][0].d1);
$('#T_weather').html(window.SWther.w[city][0].s1);
var T_weather_img=dis_img(window.SWther.w[city][0].s1);//今天天气的参数传递
$('#T_weather_img').html("<img src='"+T_weather_img+"' title='"+window.SWther.w[city][0].s1+"' alt='"+window.SWther.w[city][0].s1+"' />");
$('#T_temperature').html(window.SWther.w[city][0].t1+'° ~ '+window.SWther.w[city][0].t2+' °');
$('#T_wind').html(window.SWther.w[city][0].p1);
$('#T_direction').html(window.SWther.w[city][0].d1);
$('#M_weather').html(window.SWther.w[city][1].s1);
var M_weather_img=dis_img(window.SWther.w[city][1].s1);
$('#M_weather_img').html("<img src='"+M_weather_img+"' title='"+window.SWther.w[city][1].s1+"' alt='"+window.SWther.w[city][1].s1+"' />");
$('#M_temperature').html(window.SWther.w[city][1].t1+'° ~ '+window.SWther.w[city][1].t2+' °');
$('#M_wind').html(window.SWther.w[city][1].p1);
$('#M_direction').html(window.SWther.w[city][1].d1);
$('#L_weather').html(window.SWther.w[city][2].s1);
var L_weather_img=dis_img(window.SWther.w[city][2].s1);
$('#L_weather_img').html("<img src='"+L_weather_img+"' title='"+window.SWther.w[city][2].s1+"' alt='"+window.SWther.w[city][2].s1+"' />");
$('#L_temperature').html(window.SWther.w[city][2].t1+'° ~ '+window.SWther.w[city][2].t2+' °');$('#L_wind').html(window.SWther.w[city][2].p1);
$('#L_direction').html(window.SWther.w[city][2].d1);
}
}
var uisy={
query:function(ent){//切换状态
if(ent=='search'){
$('#weather_sub >input').disabled===true;
$('#weather_res_case').fadeOut(500,function(){$(this).addClass('common_hide')})}
else if(ent=='return'){//出现下面的元素
$('#weather_sub >input').disabled===false;
$('#weather_res_case').fadeIn(500,function(){$(this).removeClass('common_hide')})}}
}
var onEvent={
onEvent:function(){
var has_daluzai=onEvent.daluzai();
if(has_daluzai===true){
$('#weather_sub >input').click(function(){onEvent.searchWeather();})
Enter_Ctrl('weather_search',13,"onEvent.searchWeather()");}},
searchWeather:function(){
uisy.query('search');
var city=$('#weather_search >input').val();
city=$.trim(city);weather_.getWeather(city);},
daluzai:function(){
var has_author=$('#weather_author');
if(has_author.length===0){return false;}
else{has_author.click(function(){go_URL('blank','http://www.cnblogs.com/blue-lg/archive/2012/03/04/2379564.html');})
return true;}},
killError:function(){
function killErrors(){return true;}
window.οnerrοr=killErrors;}
}
//页面以某方式跳转到某页面
function go_URL(type,url){if(type=='blank'){window.open(url,'');}else{window.location=url;}}
//利用keycode=13使得回车与点击查询按钮功能一致
function Enter_Ctrl(get_id,ent,fun){
if(typeof(get_id)=='undefined'||get_id==''){//若get_id对象非空,则绑定keypress事件!
var set_obj=$(document);}
else{var set_obj=$('#'+get_id);}
if(ent==13){
set_obj.keypress(function(e){if(e.which==13||e.which==10){eval(fun);}})}
else{set_obj.keypress(function(e){if(e.ctrlKey&&e.which==13||e.which==10){eval(fun);}})}
}
//
function def_value(get_id,msg){//输入框的默认提示
var set_obj=$('#'+get_id).find('input');
if(set_obj.val()==""){
set_obj.val(msg);
set_obj.css('color','#949494');
}
set_obj.click(function(){
if($(this).val()==msg){
$(this).val('');
set_obj.css('color','');
}});
set_obj.bind("blur",function(){
if($(this).val()==''){
$(this).val(msg);
set_obj.css('color','#949494');
}});
}
注意点:
1.核心技术在于通过
$.getScript("http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=2&city="+city+"&dfc=3",function(){echo(city);});}
function echo(city){
//console.log(window.SWther.w[city]);
if(window.SWther.w[city]===undefined){alert('请输入一个正确的城市名称');return false;}
/* 剩下的就是对window.SWther.w[city][0]进行复制操作了*/
//
//
//
}
利用firebug,输出window.SWther.w[city]对象的值,发现结果如下所示:
该对象的d1/d2等值分别对应白天和晚上的天气状况,据此我们就可以轻松的使用html()进行赋值,以及图片的对应选择!
s: 天气情况
d: 风向
p: 风力情况
t: 气温
2.文档使用了jquery.js,主要是利用了其中的fadein和fadeout显示效果,读者可以自行添加此js文件。
3.文件下载地址: