DOM及AJAX实现天气预报查询
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>我爱你</title>
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/index.css" />
</head>
<body onkeypress="goSky(event)">
<div class="wrap" id="app" >
<div class="search_form">
<div class="logo"><img src="img/logo.png" alt="logo" /></div>
<div class="form_group">
<input type="text" id="city" name="mycity" value="" class="input_txt" placeholder="请输入查询的天气" />
<button class="input_sub" onclick="sendAjax()" >
搜 索
</button>
</div>
<div class="hotkey">
<a href="javascript:; " onclick="GetBj()" id="bj">西安</a>
<a href="javascript:;" onclick="GetSS()" id="ss">榆林</a>
<a href="javascript:;" onclick="GetGZ()" id="gz">铜川</a>
<a href="javascript:;" onclick="GetSZ()" id="sz">长春</a>
</div>
</div>
<ul class="weather_list" id="myUL">
<!-- <li v-for="item in weatherList">
<div class="info_type"><span class="iconfont">晴</span></div>
<div class="info_temp">
<b>低温 16℃</b>
~
<b>高温 30℃</b>
</div>
<div class="info_date"><span>18日星期五</span></div>
</li>
-->
</ul>
</div>
</body>
</html>
<script type="text/javascript">
var xmlhttp = new XMLHttpRequest();
function sendAjax() {
var cityName = document.getElementById("city").value;
if (!cityName) {
alert("请输入一个城市名")
return;
}
if (cityName.length < 2) {
alert("请输入一个有效城市名")
return;
}
xmlhttp.open("GET", "http://wthrcdn.etouch.cn/weather_mini?city=" + cityName, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsonStr = xmlhttp.responseText;
var jsonObj = JSON.parse(jsonStr);
showData(jsonObj);
}
}
}
var myUL=document.getElementById("myUL");
function showData(jsonObj) {
var arr = jsonObj.data.forecast;
var yes=jsonObj.data.yesterday;
var strli='';
for(var i=0;i<arr.length;i++){
var obj=arr[i];
strli+=`<li v-for="item in weatherList">
<div class="info_type"><span class="iconfont">${obj.type}</span></div>
<div class="info_temp">
<b>${obj.low}</b>
~
<b>${obj.high}</b>
</div>
<div class="info_date"><span>${obj.date}</span></div>
</li>`;
}
var str=`<li v-for="item in weatherList">
<div class="info_type"><span class="iconfont">${yes.type}</span></div>
<div class="info_temp">
<b>${yes.low}</b>
~
<b>${yes.high}</b>
</div>
<div class="info_date"><span>${yes.date}</span></div>
</li>`;
myUL.innerHTML=str+strli;
}
function GetBj(){
var bj= document.getElementById("bj");
bjText=bj.innerText;
getDay(bjText);
}function GetSS(){
var bj= document.getElementById("ss");
bjText=bj.innerText;
getDay(bjText);
}
function GetGZ(){
var bj= document.getElementById("gz");
bjText=bj.innerText;
getDay(bjText);
}
function GetSZ(){
var bj= document.getElementById("sz");
bjText=bj.innerText;
getDay(bjText);
}
function getDay(cityName){
xmlhttp.open("GET", "http://wthrcdn.etouch.cn/weather_mini?city=" + cityName, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsonStr = xmlhttp.responseText;
var jsonObj = JSON.parse(jsonStr);
showData(jsonObj);
}
}
}
function goSky(e){
if(e.keyCode==13){
sendAjax();
}
}
</script>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4b8ebb01897818eb3bb260730eebe246.png#pic_center)