Javascript Chrome 说无法读取未定义的属性“nodeName”,IE6 很好,使用 Google Map API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2972673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:44:57  来源:igfitidea点击:

Chrome says Cannot read property 'nodeName' of undefined, IE6 is fine, using Google Map API

javascriptxmlgoogle-mapsgoogle-chromenodename

提问by Sivakanesh

I have an XML file in the following structure.

我有以下结构的 XML 文件。

<NewDataSet>
 <markers>
  <name>name text</name>
  <desc>desc text</desc>
  <Lat>57.149328033771</Lat>
  <Long>-2.12561060173764</Long>
  <cost>9985</cost>
 </markers>
</NewDataSet>

I'm using Google Map API to parse and map these, using the following Javascript.

我正在使用 Google Map API 解析和映射这些,使用以下 Javascript。

 function load() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(47.614495, -122.341861), 13);

    GDownloadUrl("test1.xml", function(data) {
        var xml = GXml.parse(data);
        x= xml.documentElement;
        y = xml.documentElement.childNodes;
        alert(y.length);
        for (i=0;i<y.length;i++)
        {
            if (y[i].nodeType!=3)
            {
              alert(y[i].childNodes[0].nodeName + " : " + y[i].childNodes[0].childNodes[0].nodeValue); //name
            }
        }

    });
  }
}

Both the full xml and html files can be downloaded here: http://domain2405544.sites.fasthosts.com/test/test1.htmhttp://domain2405544.sites.fasthosts.com/test/test1.xml

完整的 xml 和 html 文件都可以在这里下载:http: //domain2405544.sites.fasthosts.com/test/test1.htm http://domain2405544.sites.fasthosts.com/test/test1.xml

First off, I'm just trying to parse this data. Works fine in IE6 (no choice with that) and it alerts the data correctly. However when I load the same page in Chrome is tells me:

首先,我只是想解析这些数据。在 IE6 中工作正常(没有选择)并且它正确地提醒数据。但是,当我在 Chrome 中加载同一页面时,它告诉我:

Uncaught TypeError: Cannot read property 'nodeName' of undefined - line 29

I'm using identical files, so may be there is an underlying problem I just can't see. Has anyone got any ideas why this may be happening?

我使用的是相同的文件,所以可能存在我看不到的潜在问题。有没有人知道为什么会发生这种情况?

Thanks

谢谢

回答by Tim Down

What is happening is that you're trying to get children of a text node. Chrome will have a text node prior to the <name>child of the <markers>element whereas IE won't.

发生的事情是您试图获取文本节点的子节点。Chrome 将<name><markers>元素的子元素之前有一个文本节点,而 IE 不会。

You're testing whether each child of the document element <NewDataSet>is a text node, thus avoiding the problem at that level, but you're assuming the first child of each <markers>element is the <name>element when in fact it's a text node in non-IE browsers.

您正在测试文档元素的每个子元素<NewDataSet>是否是文本节点,从而避免该级别的问题,但是您假设每个<markers>元素的第一个子元素是<name>元素,而实际上它是非 IE 浏览器中的文本节点.

You'd be better off using the getElementsByTagNameof DOM nodes to avoid this problem. Here's an improved version:

您最好使用getElementsByTagNameof DOM 节点来避免此问题。这是一个改进版本:

GDownloadUrl("test1.xml", function(data) {
    var xml = xhr.responseXML;
    var root = xml.documentElement;
    var markers = root.getElementsByTagName("markers"), nameEl;

    for (var i = 0, len = markers.length; i < len; ++i) {
        nameEl = markers[i].getElementsByTagName("name")[0];
        alert(nameEl.firstChild.data);
    }
});