Javascript 无法获取未定义或空引用的GetElementsbyTagName的属性
让我首先说明我是一个新手,虽然我已经创建了相当多的html代码,但我正在尝试转换成XML和Javascipt,以供我自己借鉴。尽管如此,我遇到了一个困扰我的问题(我研究并发现了类似的问题)。有趣的是,该代码在Firefox下运行了几次,但现在它什么都不起作用,我似乎找不到问题所在。我确实使用IE控制台检查文件打开状态等。。但是没有快乐。正如主题行所示,我似乎无法在为网页构建的表中列出XML标记。下面是相关的html代码和相关的XML文件。任何协助都将不胜感激 HTML代码Javascript 无法获取未定义或空引用的GetElementsbyTagName的属性,javascript,html,xml,getelementsbyname,Javascript,Html,Xml,Getelementsbyname,让我首先说明我是一个新手,虽然我已经创建了相当多的html代码,但我正在尝试转换成XML和Javascipt,以供我自己借鉴。尽管如此,我遇到了一个困扰我的问题(我研究并发现了类似的问题)。有趣的是,该代码在Firefox下运行了几次,但现在它什么都不起作用,我似乎找不到问题所在。我确实使用IE控制台检查文件打开状态等。。但是没有快乐。正如主题行所示,我似乎无法在为网页构建的表中列出XML标记。下面是相关的html代码和相关的XML文件。任何协助都将不胜感激 HTML代码 <script&
<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>
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”,“event.xml”,true);
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState!=4)返回;
if(xmlhttp.status==200)
警报(xmlhttp.responseText);
其他的
警报(“请求失败!”);
};
//onreadystatechange
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
文件。填写(“”);
变量y=xmlDoc.getElementsByTagName(“月”);
对于(i=0;igetElementsByTagName
返回节点列表,而不是数组,因此使用x.item(i)
要使其工作,请进行以下更改:
1) 将同步(第11行)的true更改为false
2) 声明month
变量(第31行+)
Yoyr XMLHttpRequest是异步的,这意味着在加载xmlhttp.responseXML
之前,您不能访问它。您应该在onreadystatechange
事件处理程序中完成所有工作,否则响应将无法准备就绪,除非您使用同步模式。在我进行故障排除时,实际上,我尝试了一个同步请求,摆脱了所有的文件状态检查,简化了一切,但仍然存在类似的问题。谢谢你的建议。当我阅读GetElementByTagName时,我想它确实说返回的是一个数组(这给我带来了很多问题),但感谢您澄清这一点。似乎我对代码有了进一步的了解,但现在我得到了“无法获取未定义或空引用的属性”0)。只是为了澄清上述内容,即“month1_date”好的,我修复了我遇到的问题。信不信由你,这是我的XML文件。我混合了两个不同的根(我想这是正确的术语)通过将标题所需的月份与表内容放在同一个XML文件中。我将文件(一个名为months.XML,另一个名为event.XML)分开,所有内容都按照我的原始代码所认为的方式工作。非常感谢那些试图帮助我的人,非常感谢。我是否需要关闭此选项(我在任何地方都看不到该选项)??
xmlhttp.open("GET","event.xml",false);
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); // !!!
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);