DOM及AJAX实现天气预报查询

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">
	//创建Ajax请求对象
	var xmlhttp = new XMLHttpRequest();

	function sendAjax() {
		//发送Ajax请求
		//alert("发送请求");
		var cityName = document.getElementById("city").value;
		//alert(cityName);
		if (!cityName) {
			alert("请输入一个城市名")
			return;
		}
		if (cityName.length < 2) {
			alert("请输入一个有效城市名")
			return;
		}
	
		//发送Ajax请求
		// http://wthrcdn.etouch.cn/weather_mini?city=商洛
		xmlhttp.open("GET", "http://wthrcdn.etouch.cn/weather_mini?city=" + cityName, true);
		xmlhttp.send();

		//接收服务器的响应
		// 在 onreadystatechange 事件中, 我们规定当服务器响应已做好被处理的准备时所执行的任务。

		// 当 readyState 等于 4 且状态为 200 时, 表示响应已就绪:

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var jsonStr = xmlhttp.responseText;
				//把json字符串转换成JSON对象
				var jsonObj = JSON.parse(jsonStr);
				//解析JSON
				showData(jsonObj);
			}
		}
	}
	var myUL=document.getElementById("myUL");
	function showData(jsonObj) {
		var arr = jsonObj.data.forecast;
		var yes=jsonObj.data.yesterday;
		
		//console.log(arr);
		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;
		/* x=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();
		
		//接收服务器的响应
		// 在 onreadystatechange 事件中, 我们规定当服务器响应已做好被处理的准备时所执行的任务。
		
		// 当 readyState 等于 4 且状态为 200 时, 表示响应已就绪:
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var jsonStr = xmlhttp.responseText;
				//把json字符串转换成JSON对象
				var jsonObj = JSON.parse(jsonStr);
				//解析JSON
				showData(jsonObj);
			}
		}
	}
	function goSky(e){
	/* 	alert(e.keyCode); */
	if(e.keyCode==13){
		sendAjax();
	}
		}
</script>

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值