ajax教程
AJAX = Asynchronous JavaScript and XML(異步的 JavaScript 和 XML)。
AJAX 不是新的編程語言,而是一種使用現(xiàn)有標(biāo)準(zhǔn)的新方法。
AJAX 是與服務(wù)器交換數(shù)據(jù)并更新部分網(wǎng)頁的藝術(shù),在不重新加載整個(gè)頁面的情況下。
客戶端部分:html、js、css代碼部分:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title></title><meta charset="UTF-8"/></head><!--css樣式部分--><style type="text/css">.content_map{/*border:1px solid blue;*/width:1349px;height:524px;float:left;margin-top:100px;}.content_map .mLeft{border:none;border-top:1px solid #fb6c20;width:400px;margin-top:14px;float:left;margin-left:134px;}.content_map>span{margin-left:20px;margin-right:20px;font-size:28px;font-family: "Microsoft Yahei";/*font-weight: bold;*/float:left;}.content_map .mRight{float:left;border:none;border-top:1px solid #fb6c20;width:400px;margin-top:14px;}#maplist{margin-top:50px;width:749px;height:524px;/*border:1px solid #fb6c20;*/background: url("images/diru.png") no-repeat 0 0 ;background-size:contain;position: relative;float:left;}.mapShop img{position:absolute;/*border:1px solid red;*/}#map_right{/*border:1px solid #fb6c20;*/float:left;/*width:600px;*/width:594px;height:524px;background-color: #f0f2fe;margin-top: 40px;}.shopMsg img{width:450px;height:300px;margin-left:72px;margin-top:40px;}.shopMsg .pmname{color:#000;font-size:20px;margin-top:30px;margin-left:72px;font-family:微軟雅黑;}.shopMsg .address{color:#000;font-size:20px;margin-top:30px;margin-left:72px;font-family:微軟雅黑;}.shopMsg .phone{color:#000;font-size:20px;margin-top:30px;margin-left:72px;font-family:微軟雅黑;}</style><body><!--html部分--><div class="content_map"><!-- 標(biāo)題--><hr class="mLeft"/><span>相關(guān)寵物醫(yī)院</span><hr class="mRight"/><!-- 左邊部分:地圖--><div id="maplist"></div><!-- 右邊部分點(diǎn)擊左邊要添加的內(nèi)容:以及最開始加入的信息--><div id="map_right"><div class="shopMsg"><img src="images/w_map.png"/><div class="pmname">寵物店名:Petjoy寵物社區(qū)</div><div class="address">地址:長寧區(qū)機(jī)旋路1258號(hào)--1260號(hào)</div><div class="phone">電話號(hào)碼:(021)53018000</div></div></div></div><!--js代碼部分--><script type="text/javascript">window.onload=function(){getMap();}// 向地圖添加信息:ajaxfunction getMap(){//創(chuàng)建對(duì)象var httpReq;if(window.XMLHttpRequest){httpReq=new XMLHttpRequest();}else{httpReq=new ActiveXObject("Microsoft.XMLHTTP");}var maplist=document.getElementById("maplist");//獲取地圖列表maplist.innerHTML='';//清空地圖里在html里面加的信息// 定義回調(diào)函數(shù),接收從數(shù)據(jù)庫響應(yīng)回來的數(shù)據(jù)。// onreadystatechange():存儲(chǔ)函數(shù)(或函數(shù)名)。每當(dāng)readyState屬性改變時(shí),就會(huì)調(diào)用該函數(shù)httpReq.onreadystatechange=function(){if(httpReq.readyState==4&&httpReq.status==200){var jsonobj=JSON.parse(httpReq.responseText);console.log(jsonobj.length);for (var i = 0; i< jsonobj.length;i++) {maplist.innerHTML+='<div class="mapShop">'+'<img src="images/fi1.png" style="top:'+jsonobj[i].pmTop+"px"+';left:'+jsonobj[i].pmLeft+"px"+'"/>'+'<div id="pmcity'+i+'" onclick="getMessage('+i+')" style="top:'+jsonobj[i].pmTop+"px"+';left:'+jsonobj[i].pmLeft+"px"+';position:absolute;padding-top:20px;'+'">' + jsonobj[i].pmCity + '</div>'+'</div>';}}}//發(fā)起請(qǐng)求(打開一個(gè)地址)httpReq.open("get", "adress.do", true);//發(fā)送,如果提交方式為get,發(fā)送為null;如果提交方式為post,哪send里寫要發(fā)送的參數(shù),沒得的話,就寫nullhttpReq.send(null);}//點(diǎn)擊獲取信息function getMessage(a){console.log("M----------1");var httpReq;if(window.XMLHttpRequest){httpReq=new XMLHttpRequest();}else{httpReq=new ActiveXObject("Microsoft.XMLHTTP");}var map_right=document.getElementById("map_right");map_right.innerHTML='';httpReq.onreadystatechange=function(){if(httpReq.readyState==4&&httpReq.status==200){var jsonobj=JSON.parse(httpReq.responseText);console.log(jsonobj.length);for(var i=0;i<jsonobj.length;i++){map_right.innerHTML+='<div class="shopMsg">'+'<img src="images/'+jsonobj[i].pmImg+'"/>'+'<div class="pmname">寵物店名:'+jsonobj[i].pmName+'</div>'+'<div class="address">地址:'+jsonobj[i].pmAddress+'</div>'+'<div class="phone">電話號(hào)碼:'+jsonobj[i].pmPhone+'</div>'+'</div>'}}}//發(fā)起請(qǐng)求httpReq.open("get", "adressMsg.do?pmId="+a, true);//發(fā)送httpReq.send(null);}</script></body></html>
新聞熱點(diǎn)
疑難解答
圖片精選