Ajax对xml信息的接收和处理操作实例分析(ajax渲染页面完毕)满满干货

随心笔谈11个月前发布 admin
96 0


<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title></title>
<script type=”text/javascript”>
function f1(){
//ajax请求xml信息
//JavaScript的dom技术处理xml
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
//alert(xhr.responseXML);//object Document 其是xml根节点的父节点对象
var xmldom=xhr.responseXML;
//console.log(xmldom.firstChild);//<weather>
var citys=xmldom.getElementsByTagName(‘city’);
//console.log(citys);// HTMLCollection[city, city, city]

var s=””;
for(var i=0; i<citys.length; i++){
var name=citys[i].getElementsByTagName(‘name’)[0].firstChild.nodeValue;
var temp=citys[i].getElementsByTagName(‘temp’)[0].firstChild.nodeValue;
var wind=citys[i].getElementsByTagName(‘wind’)[0].firstChild.nodeValue;
s +=”城市:” + name + “--温度:” + temp + “--风向:” + wind + “<br />”;
}
document.getElementById(‘result’).innerHTML=s;
}
}
xhr.open(‘get’,’https://www.jb51.net/article/09.xml’);
xhr.send(null);
}
</script>
</head>
<body>
<h2>利用Ajax+JavaScript实现对xml的接收和处理</h2>
<input type=”button” value=”触发” onclick=”f1()” />
<div id=”result”></div>
</body>
</html>

© 版权声明

相关文章