$.get("http://wthrcdn.etouch.cn/WeatherApi", { citykey: cityCode }, function (d) { //创建文档对象 var parser = new DOMParser(); var xmlDoc = parser.parseFromString(d, "text/xml"); var cityList = xmlDoc.getElementsByTagName('city'); var city = $(cityList[0]).text(); var weatherList = xmlDoc.getElementsByTagName('weather'); var $weather = $(weatherList[0]); var date = $weather.children("date").text();//时间 var high = $weather.find("high").text();//最高气温 var low = $weather.find("low").text();//最低气温 var dayType = $weather.find("day type").text();//白天天气 var nightType = $weather.find("night type").text();//晚上天气 var html = city + "<label style='10px;'></label>" + "白天:" + dayType + ",晚上:" + nightType + "<label style='10px;'></label>" + low + "<label style='10px;'></label>" + high; var aWeather = document.getElementById("aWeather"); aWeather.innerHTML = html; aWeather.setAttribute("href", "http://www.weather.com.cn/weathern/" + cityCode + ".shtml"); });
接口调用:
http://wthrcdn.etouch.cn/WeatherApi?city=昆明
或者
http://wthrcdn.etouch.cn/WeatherApi?citykey=citycode