<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Mozilla, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5, IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=onResponse;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function onResponse()
{
if(xmlhttp.readyState!=4) return;
if(xmlhttp.status!=200)
{
alert("Problem retrieving XML data");
return;
}
txt="<table border='1'>";
x=xmlhttp.responseXML.documentElement.getElementsByTagName("CD");//这行是什么意思??其中的CD又是什么意思??之前看到的都是document,这个documentElement是什么意思??
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('copy').innerHTML=txt;
}
</script>
</head>
<body>
<p id="copy">
<button onclick="loadXMLDoc('/example/xmle/cd_catalog.xml')">Get CD info</button>
</p>
</body>
</html>
The code above is a sample code in W3CSchool that I saw when I was learning AJAX from w3cschool. The purpose is to display the XML file as an HTML table. However, I always felt that something was wrong, so I googled it for a long time. But I still don’t understand it. Please explain to me the issues mentioned in my comments.
滿天的星座2017-05-19 10:42:33
Document refers to the document model, documentElement is the element
x=xmlhttp.responseXML.documentElement.getElementsByTagName("CD");//这行是什么意思??其中的CD又是什么意思??之前看到的都是document,这个documentElement是什么意思??
refers to getting the collection named "CD" in the returned XML
txt=txt + "" + xx[0].firstChild.nodeValue + "";//这行什么意思???
xx=x[i].getElementsByTagName("TITLE");
refers to the content of the first child element under the tag TITLE
漂亮男人2017-05-19 10:42:33
xmlhttp
是你的ajax返回的对象,同样的,后面的responseXML
,documentElement
is the corresponding method under the previous object.
And the code behind getElementsByTagName
类比于找到所有tag
为CD
的内容。如果你能看到ajax请求回来的xml的话,你就能在中间找到<CD></CD>
is like this.
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
作用是拿到 title
下的第一个节点的值插入到td
Inside the tag
txt=txt + "</table>"
当然不是加一个就自动补全,这个是闭合标签,前面起始的txt="<table border='1'>";
tags,