【问题标题】:jquery Cannot read property 'getAttribute' of undefinedjquery无法读取未定义的属性'getAttribute'
【发布时间】:2015-03-16 21:17:29
【问题描述】:

我使用的 javascript 在 firefox 和 ie 中运行良好,但在 chrome 和 safari 上运行时出现此错误。我不完全确定它为什么会失败。

        var response = asyncResult.value;
        if (window.DOMParser) {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(response, "text/xml");

        }
        else 
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(response);
        }

        console.log(xmlDoc);
         var changeKey = xmlDoc.getElementsById("t:ItemId")[0].getAttribute("ChangeKey");

控制台显示此消息,但当我将其设置为 console.log() 时输出 xmlDoc 就好了


Uncaught TypeError: Cannot read property 'getAttribute' of undefined        r.js 
soapToGetItemDataCallback    r.js
r.onreadystatechange    outlookwebapp-15.js:21 
$h.EwsRequest.$1x_1    outlookwebapp-15.js:21 
(anonymous function)   outlookwebapp-15.js:21

【问题讨论】:

  • 你的console.log(xmlDoc);是否输出了预期的结果?
  • 是的,在 Firefox 和 chrome 中都可以
  • getElementsById 是无效的方法,因此在所有浏览器中都应该失败。也许它在 Chrome 和 Safari 中的失败与在 IE 和 Firefox 中不同。
  • 你说得对,我实际上复制并粘贴了错误的版本。有一次,有人建议我尝试“byId”。我实际上最初有 getElementsByTagName,这是原始错误的来源。有什么想法吗?
  • 使用getElementsByTagName 可以解决您的错误吗?

标签: javascript jquery soap exchangewebservices


【解决方案1】:

问题是你试图通过 ID 获取元素并使用[0],我猜你想要getElementsByTagName,因为结果未定义,代码应该是:

var changeKey = xmlDoc.getElementsById("t:ItemId").getAttribute("ChangeKey");

或者如果"t:ItemId" 是一个集合:

 var changeKey = xmlDoc.getElementsByTagName("t:ItemId")[0].getAttribute("ChangeKey");

【讨论】:

  • 谢谢,我居然复制粘贴错了版本。有一次,有人告诉我尝试“byId”。实际上,我最初有 getElementsByTagName,这是原始错误的来源。
猜你喜欢
  • 1970-01-01
  • 2022-06-28
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2021-11-26
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多