繁体   English   中英

jQuery无法读取未定义的属性“ getAttribute”

[英]jquery Cannot read property 'getAttribute' of undefined

我正在使用的javascript在firefox中完全可以正常工作,即在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");

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


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

问题是您正在尝试通过ID并使用[0]来获取元素,我想您想使用getElementsByTagName因为结果是未定义的,代码应为:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM