jQuery访问xml文件显示天气预报

来源:互联网 发布:java 在线客服 编辑:程序博客网 时间:2024/05/22 17:03

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Start jQuery</title>

<script src="lib/jquery-1.2.6.min.js" type="text/javascript"></script>

<script type="text/javascript">

//http://www.jb51.net/article/16931.htm

//weather.xml

$(document).ready(function(){ 

$.ajax({ 

url:"http://www.google.com/ig/api?weather=suzhou", 

dataType:"xml", 

error: function(xml){ 

alert('Error loading XML document'+xml); 

}, 

success:function(xml){ 

var str="";

$(xml).find("weather > forecast_information > postal_code").each(function(){ 

//alert("text:"+$(this).find("content").text());//each是循环执行,即多次弹出。 

//取得属性的方法

str= "城市代码:"+$(this).attr("data") ;

}); 

str+="<br>日期:"+$("weather > forecast_information > forecast_date", xml).attr("data") ;

str+="<br>天气:"+$("weather > current_conditions > condition", xml).attr("data") ;

 

$(xml).find("weather > forecast_conditions").each(function(){ 

//取得属性的方法 

str+="<br>日期:"+$(this).find("day_of_week").attr("data");

str+=" 温度:"+$(this).find("low").attr("data")+"-"+$(this).find("high").attr("data");

str+=" 天气:"+$(this).find("condition").attr("data");

 

}); 

 

 $("#rating").html(str);

}) 

}); 

 

</script>

</head>

<body>

<div id="rating">请稍候,正在加载......</div>

</body></html>