无法读取未定义的属性“ childNodes” [英] Cannot read property 'childNodes' of undefined

查看:470
本文介绍了无法读取未定义的属性“ childNodes”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对这样的xml文件运行请求:

I am running a request to an xml file like this :

function load()
{
    var request = new XMLHttpRequest();
    request.open("GET", "response.xml", true);
    request.send();

    request.onreadystatechange = function ()
    {
        if (request.readyState == 4)
        {
            var x = request.responseXML;
            var y = x.getElementsByTagName("Limits")[0].childNodes[1];
            var z = y.getElementsByTagName("Limits")[0].childNodes[0];
            document.write(z);
        }
    };
}

xml response.xml 是这样:

<Factory>
    <Limits>
        <Point X="92" Y="489">hahahahff</Point>
        <Point X="570" Y="487">1111</Point>
        <Point X="570" Y="138">33333</Point>
        <Point X="92" Y="140">44444444</Point>
        <Point X="92" Y="139">5555555</Point>
    </Limits>
    <Cells>
        <Cell>
            <Point X="358" Y="138"/>
            <Point X="361" Y="487"/>
            <Point X="570" Y="487"/>
            <Point X="570" Y="138"/>
            <Point X="358" Y="138"/>
        </Cell>
        <Cell>
            <Point X="311" Y="139"/>
            <Point X="311" Y="488"/>
            <Point X="92" Y="489"/>
            <Point X="92" Y="140"/>
            <Point X="311" Y="139"/>
        </Cell>
    </Cells>
</Factory>

我收到此错误:


无法读取未定义的属性'childNodes'

为什么会这样?

我要实现的是不使用打印第二个 POINT 节点的值getElementsByTagName( POINT)[1]

What I am trying to achieve is print the value of the 2nd POINT node WITHOUT using getElementsByTagName("POINT")[1].

基本上我想输入 Limits节点,并打印第二个值。

Basically I want to 'enter' the Limits node, and print the 2nd value.

推荐答案

此行:

var z=y.getElementsByTagName("Limits")[0].childNodes[0];

应为:

var z=x.getElementsByTagName("Limits")[0].childNodes[0];

x 是上一行的内容,因此它已经是 Limits 的子级。因此,此行试图在 Limits 的子项内查找 Limits ,并且没有嵌套的限制

x is what got from the previous line, so it's already a child of Limits. So this line was trying to find Limits inside the child of Limits, and there is no nested Limits like that.

这篇关于无法读取未定义的属性“ childNodes”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆