AJAX XML 实例(w3school)

来源:互联网 发布:mac 刷机iphone 编辑:程序博客网 时间:2024/06/07 16:31
<html><head><script type="text/javascript">function loadXMLDoc(url){var xmlhttp;var txt,x,xx,i;if (window.XMLHttpRequest)//判断浏览器  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()//onreadystatechange在async=true(异步)的情况下使用  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    txt="<table border='1'><tr><th>Title</th><th>Artist</th></tr>";    x=xmlhttp.responseXML.documentElement.getElementsByTagName("CD");    for (i=0;i<x.length;i++)      {      txt=txt + "<tr>";      xx=x[i].getElementsByTagName("TITLE");        {        try          {          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";          }        catch (er)          {          txt=txt + "<td> </td>";          }        }      xx=x[i].getElementsByTagName("ARTIST");        {        try          {          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";          }        catch (er)          {          txt=txt + "<td> </td>";          }        }      txt=txt + "</tr>";      }    txt=txt + "</table>";    document.getElementById('txtCDInfo').innerHTML=txt;    }  }xmlhttp.open("GET",url,true);xmlhttp.send();}</script></head><body><div id="txtCDInfo"><button onclick="loadXMLDoc('/example/xmle/cd_catalog.xml')">获得 CD 信息</button></div></body></html>


http://www.w3school.com.cn/ajax/ajax_xmlfile.asp

1 0
原创粉丝点击