javascript - JavaScript 中的未定义错误

标签 javascript html xml

我正在从 XML 获取数据。我可以成功地从 XML 中获取价格,但当我使用下面给出的函数时,会出现一个名为 undefined 的意外错误;

<html>
  <head>
    <script type="text/javascript">
      function myXml(origin, destination) {
        var x=xmlDoc.getElementsByTagName("flights");

        for(i=0;i<x.length;i++) {
          if(x[i].getAttribute('FrTLAs')==origin && x[i].getAttribute('destination')==destination) {
            document.write(x[i].getAttribute('price'))
          }
        }
      }
    </script>
  </head>
  <body>
    <script type="text/javascript">
      document.write(myXml('SYD','Bali'));
    </script>
  </body>
</html>

最佳答案

myXml('SYD','Bali') 调用返回 undefined,因为您在函数体内没有返回任何内容。所以

document.write(myXml('SYD','Bali'));

将打印 "undefined" 。只需将上面的代码替换为:

myXml('SYD','Bali');

关于javascript - JavaScript 中的未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10152952/

相关文章:

python - HTML 从 python 列表中选择选项

html - 全宽响应式网站正文图片

java - XML (XSL) 错误 — 将 HTML 转换为 PDF (Java)

c# - 使用 SelectSingleNode 处理 Null 异常

javascript - Ghostscript 是否支持将 JavaScript 插入到现有的 PDF 中?

javascript - 在数组对象中查找一个值并使用该对象中的另一个值

javascript - 在 jquery 中返回到以前的状态

javascript - 使用 socket.io 和 Express JS 的聊天应用程序

javascript - PDF.js 字体加载期间出错

c++ - 在 C++ 中读取 XML 文件