javascript - Learning some issues found in AJAX
ringa_lee
ringa_lee 2017-05-19 10:40:33
0
2
593
<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.

ringa_lee
ringa_lee

ringa_lee

reply all(2)
滿天的星座

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

漂亮男人
  1. xmlhttp是你的ajax返回的对象,同样的,后面的responseXML,documentElement is the corresponding method under the previous object.

  2. And the code behind getElementsByTagName类比于找到所有tagCD的内容。如果你能看到ajax请求回来的xml的话,你就能在中间找到<CD></CD> is like this.

  3. txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";作用是拿到 title 下的第一个节点的值插入到tdInside the tag

  4. txt=txt + "</table>"当然不是加一个就自动补全,这个是闭合标签,前面起始的txt="<table border='1'>"; tags,

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!