duanjia4220 2014-06-30 11:36
浏览 75
已采纳

Safari问题,变量未定义(XML / Javascript)

I have this piece of code, it works in all browsers, except for Safari:

        // Create a connection to the file.
        var Connect = new XMLHttpRequest();

        // Define which file to open and
        // send the request.
        Connect.open("GET", "<?php echo $fileName;?>", false);
        Connect.setRequestHeader("Content-Type", "text/xml");
        Connect.setRequestHeader("Cache-Control", "no-cache");
        Connect.send();

        // Place the response in an XML document.
        var TheDocument = Connect.responseXML;

        // Place the root node in an element.
        var questions = TheDocument.childNodes[0];

        // Retrieve each customer in turn.
        for (var i = 0; i < 1; i++) {
            console.log(i);
            console.log("fake var");
            var question = questions.children[0];
            console.log(question);
        }

Variable $filename is the php link to the XML file i'm reading. I have the following issue, the code runs into the For loop, i can log var "i", and "fake var". But once i define var question as questions.children[0], the code stops. I cant log the question var in safari.

Any ideas? ( Feedback also welcome, still learning javascript/xml )

Example of my XML:

<?xml version='1.0' encoding="UTF-8" ?>
<Beantwoordt_kritiek id="1" admin="Jan Hut" categorie="Omgaan met elkaar" winswf="images/Kado (1).jpg">
  <gebruikers_vraag1>
    <vraag><![CDATA[Sleep de puzzelstukken naar het juiste vakje.]]></vraag>
    <s_vraag><![CDATA[sounds/puzzel.mp3]]></s_vraag>
    <thumb1><![CDATA[images/527puzzel1_1.jpg]]></thumb1>
    <thumb2><![CDATA[images/527puzzel1_2.jpg]]></thumb2>
    <thumb3><![CDATA[images/527puzzel1_3.jpg]]></thumb3>
    <thumb4><![CDATA[images/527puzzel1_4.jpg]]></thumb4>
    <vraagFoto><![CDATA[images/527puzzel1.jpg]]></vraagFoto>
  </gebruikers_vraag1>
</Beantwoordt_kritiek>
  • 写回答

1条回答 默认 最新

  • doutongwei4380 2014-06-30 12:07
    关注

    You could use this script, so safari give back the same result.

    <script>
    function getChildren(element) {
      if (element.children == undefined) {
        var childNodes = element.childNodes
        var children = []
        for(var i = 1; i < childNodes.length; i += 2) {  // take every second element
          children.push(childNodes[i]);
        }
        return children;
      } else {
        return element.children
      }
    }
    </script>
    

    and then call this in your script

    var question = getChildren(questions)[0];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接