Javascript:childNodes.length 显示太多元素?

发布于 2024-11-20 00:25:09 字数 2241 浏览 0 评论 0原文

我在 JavaScript 中解析一些 XML 时得到了一些奇怪的结果。我在外部文件中有以下 XML:

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

<alerts>
 <alert><![CDATA[ This is the first alert message in a series... ]]></alert>
 <alert><![CDATA[ This is the second alert message in a series, which features a <a href="http://www.facebook.com" target="blank">hyperlink</a>... ]]></alert>
 <alert><![CDATA[ This is the third alert message in a series, which features <span class="emphasizedAlertText">text formatted via a css rule</span> ]]></alert>
 <alert><![CDATA[ This is the fourth alert message in a series, which features a <span class="fauxHyperlink" onclick="someFunction();">javascript call</span>... ]]></alert>
</alerts>

通过 XMLHttpRequest 抓取文件后,我有以下函数将其输出到页面:

function testFunc()
{
   var xhrRsp = 'failed to initialize';
   rslt = document.getElementById('rslt');
   if(xhr.readyState == 4)
      {
         if(xhr.status == 200)
         {
            xhrRsp = xhr.responseXML;
            xhrTree = xhrRsp.documentElement.childNodes;
            rslt.innerHTML = xhrRsp + ' (' + xhrTree.length + ' nodes) ';
            for(var i = 0; i < xhrTree.length; i++)
            {
               if(xhrTree[i].nodeName != '#text')
               {
                  rslt.innerHTML = rslt.innerHTML + '<br/>' + xhrTree[i].nodeName + ' (' + xhrTree[i].nodeType + ') ' + ' = ' + xhrTree[i].childNodes[0].nodeValue;
               }
            }
            //rslt.innerHTML = outMsg;
         }
   }
   else
   {
      xhrRsp = 'Loading...';
   }
}

当我在浏览器中运行脚本时,我得到的结果是:

Click Me
[object Document] (9 nodes) 
alert (1) = This is the first alert message in a series...
alert (1) = This is the second alert message in a series, which features a hyperlink...
alert (1) = This is the third alert message in a series, which features text formatted via a css rule
alert (1) = This is the fourth alert message in a series, which features a javascript call...

我很困惑为什么节点数返回为 9,以及为什么其他节点都以 #text 形式出现,我目前正在上面的脚本中过滤掉这些内容。我环顾四周,无法弄清楚我错过了什么。

提前致谢!

I'm getting some weird results while parsing some XML in JavaScript. I have the following XML in an external file:

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

<alerts>
 <alert><![CDATA[ This is the first alert message in a series... ]]></alert>
 <alert><![CDATA[ This is the second alert message in a series, which features a <a href="http://www.facebook.com" target="blank">hyperlink</a>... ]]></alert>
 <alert><![CDATA[ This is the third alert message in a series, which features <span class="emphasizedAlertText">text formatted via a css rule</span> ]]></alert>
 <alert><![CDATA[ This is the fourth alert message in a series, which features a <span class="fauxHyperlink" onclick="someFunction();">javascript call</span>... ]]></alert>
</alerts>

After grabbing the file via XMLHttpRequest, I have the following function to output it to the page:

function testFunc()
{
   var xhrRsp = 'failed to initialize';
   rslt = document.getElementById('rslt');
   if(xhr.readyState == 4)
      {
         if(xhr.status == 200)
         {
            xhrRsp = xhr.responseXML;
            xhrTree = xhrRsp.documentElement.childNodes;
            rslt.innerHTML = xhrRsp + ' (' + xhrTree.length + ' nodes) ';
            for(var i = 0; i < xhrTree.length; i++)
            {
               if(xhrTree[i].nodeName != '#text')
               {
                  rslt.innerHTML = rslt.innerHTML + '<br/>' + xhrTree[i].nodeName + ' (' + xhrTree[i].nodeType + ') ' + ' = ' + xhrTree[i].childNodes[0].nodeValue;
               }
            }
            //rslt.innerHTML = outMsg;
         }
   }
   else
   {
      xhrRsp = 'Loading...';
   }
}

When I run the script in the browser, I get this as the result:

Click Me
[object Document] (9 nodes) 
alert (1) = This is the first alert message in a series...
alert (1) = This is the second alert message in a series, which features a hyperlink...
alert (1) = This is the third alert message in a series, which features text formatted via a css rule
alert (1) = This is the fourth alert message in a series, which features a javascript call...

I'm puzzled as to why the number of nodes comes back as 9, and why every other one of these comes in as #text, which I'm currently filtering out in the above script. I've been looking around and can't figure out what I'm missing.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

死开点丶别碍眼 2024-11-27 00:25:09

这是符合 W3C 的浏览器的正确行为。元素之间的空白作为 TextNode 包含在 DOM 中。

您可能会发现在 IE8 及以下版本中,不会生成文本节点。这是因为这些浏览器不遵守这方面的规范。

This is the correct behavior for W3C compliant browsers. White space between elements is included in the DOM as a TextNode.

You'll probably find that in IE8 and below, the text nodes are not generated. This is because those browsers do not adhere to the specification in this regard.

辞旧 2024-11-27 00:25:09

我敢打赌,您在一个蹩脚的浏览器上运行它,它不会按照应有的方式处理空格。

不管怎样,你正在用 if 过滤一些节点,所以这并不奇怪。一些快速记录可以非常快地解决您的问题。 。 。

I bet you run that on a crappy browser that doesn't process whitespaces as they should.

Anyway, you are filtering some node with your if, so this is not surprising. Some quick logging could answer your problem very fast . . .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文