javascript - 无法获取未定义或空引用的 GetElementsbyTagName 属性

标签 javascript html xml getelementsbyname

首先我要说明的是,我是一名新手,虽然我已经创建了相当多的 html 代码,但我正在尝试转向 XML 和 Javascipt 以进行 self 启发。话虽这么说,我遇到了一个困扰我的问题(我研究并发现了类似的问题)。有趣的是,这段代码在 Firefox 下运行了几次,但现在却不起作用,我似乎找不到问题所在。我确实使用 IE 控制台来检查文件打开状态等..但没有乐趣。正如主题行所示,我似乎无法在为网页构建的表中列出 XML 标记。这是相关的 html 代码和关联的 XML 文件。任何帮助将不胜感激。

HTML 代码

<script>
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.open("GET","event.xml",true);

xmlhttp.onreadystatechange=function()
 {
  if(xmlhttp.readyState!=4)return;
  if(xmlhttp.status==200)
     alert(xmlhttp.responseText);
 else
     alert("Request failed!");
};
//onreadystatechange

xmlhttp.send();

xmlDoc=xmlhttp.responseXML;

document.write("<table border='1'>");

var y=xmlDoc.getElementsByTagName("months");

for (i=0;i<y.length;i++)
{
 document.write("<tr bgcolor='#6CA6CD'>");
 document.write("<th>");
 document.write(month[i].getElementsByTagName("month1")[0].childNodes[0].nodeValue);
 document.write("</th>");
 document.write("<th>");
 document.write(month[i].getElementsByTagName("month2")[0].childNodes[0].nodeValue);
 document.write("</th>");
 document.write("</tr>");
}


var x=xmlDoc.getElementsByTagName("month_event");
for (i=0;i<x.length;i++)
  {
  document.write("<tr><td bgcolor='white'>");
  document.write("<b> ");
  document.write(x[i].getElementsByTagName("month1_date")[0].childNodes[0].nodeValue);
  document.write("</b> - ");
  document.write(x[i].getElementsByTagName("month1_day")[0].childNodes[0].nodeValue);
  document.write(" - ");
  document.write(x[i].getElementsByTagName("month1_time")[0].childNodes[0].nodeValue);
  document.write(" - ");
  document.write(x[i].getElementsByTagName("month1_evdescription")[0].childNodes[0].nodeValue); 

  document.write("</td><td bgcolor='lightgrey'>");
  document.write("<b> ");
  document.write(x[i].getElementsByTagName("month2_date")[0].childNodes[0].nodeValue);
  document.write("</b> - ");
  document.write(x[i].getElementsByTagName("month2_day")[0].childNodes[0].nodeValue);
  document.write(" - ");
  document.write(x[i].getElementsByTagName("month2_time")[0].childNodes[0].nodeValue);
  document.write(" - ");
  document.write(x[i].getElementsByTagName("month2_evdescription")[0].childNodes[0].nodeValue);

  document.write("</td></tr>");
  }
document.write("</table>");
</script>

XML File:
<?xml version="1.0" encoding="UTF-8"?>

<events>   
  <months> 
    <month1>December</month1>
    <month2>January</month2>    
  </months>

 <month_event>
  <month1_date>22 Dec</month1_date>
  <month1_day>Sunday</month1_day>
  <month1_time>10:30AM - 11:00AM</month1_time>
  <month1_evdescription>The Centre is closed</month1_evdescription>
  <month2_date>6 Jan</month2_date>
  <month2_day>Tuesday</month2_day>
  <month2_time>All Day</month2_time>
  <month2_evdescription>The Heavy</month2_evdescription>
</month_event>

<month_event>
  <month1_date>25 Dec</month1_date>
  <month1_day>Wednesday</month1_day>
  <month1_time>All Day</month1_time>
  <month1_evdescription>Christmas</month1_evdescription>
  <month2_date>10 Jan</month2_date>
  <month2_day>Tuesday</month2_day>
  <month2_time>10:30AM - 11:30AM</month2_time>
  <month2_evdescription>Nothing</month2_evdescription>
</month_event>

</events>

最佳答案

getElementsByTagName 返回 NodeList,而不是数组,因此请使用 x.item(i)

要使其正常工作,请进行以下更改:

1) 将 true 更改为 false 以进行同步(第 11 行)

xmlhttp.open("GET","event.xml",false);

2) 声明 month 变量(第 31+ 行)

for (i=0;i<y.length;i++)
{
 var month = y.item(i);   // insert this line
 document.write("<tr bgcolor='#6CA6CD'>");
 document.write("<th>");
 document.write(month.getElementsByTagName("month1").item(0).childNodes[0].nodeValue); // !!!

3) 使用 .item(i).item(0) 而不是 [i][0] (第 45 行以上)

var x=xmlDoc.getElementsByTagName("month_event");
for (i=0;i<x.length;i++)
  {
  document.write("<tr><td bgcolor='white'>");
  document.write("<b>test ");
  document.write(x.item(i).getElementsByTagName("month1_date").item(0).childNodes[0].nodeValue);

关于javascript - 无法获取未定义或空引用的 GetElementsbyTagName 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20850381/

相关文章:

xml - 我需要以文本格式与不同的程序交换数据,你有什么建议?

javascript - 如何在 typescript 中加载 Electron 模块

javascript - 具有唯一值的数组 : indexOf vs new Set

javascript - 使用 javascript 在输入字段上只接受 20 的倍数(例如 40、60、80)?

javascript - 当我第二次访问它时,HTML Div 元素变为空?

javascript - 带有数据的 XML 模式?我如何用JS解析它?

c# - 使用 LINQ to XML 从 WSDL 解析 xsd

javascript - 选择相关TD的行TH

javascript - 刷新时重定向到不同的 URL

javascript - KnpSnappyBundle 不执行 javascript