1

我一直在编写此代码以从通过 AJAX 检索的 XML 文档中动态加载照片链接。我有一个存储响应 XML 的全局变量 xDoc,我在其他函数中使用它,这样我就不必每次都加载文档。请记住,我对 JavaScript 还很陌生,所以细节会很棒。

我在 Chrome 中收到此错误:“Uncaught TypeError: Cannot call method 'getElementsByTagName' of undefined”,以及其他浏览器中的类似错误消息。但是,我的代码仍然适用于 FireFox、Chrome 和 Safari。然而,对于 IE,每个版本都无法正常工作。我无法理解出了什么问题,以及为什么该元素未定义。

错误行在下面的代码中清楚地标记。这是“eProject.link = eProject.photo.getElementsByTagName("edit")[0].firstChild.data;”

我的XML的数据结构如下:

<project>
  <stuff>
  <stuff>
  <photos>
     <photo>
       <stuff>
       <edit>
     </photo>
  </photos>
</project>

然后这是 JavaScript:

// Initialize 
function init() {
    // Set the xml file 
    loadXMLDoc("../xml/featured.xml");
}
window.onload = init;

// Define xDoc global variable
var xDoc;
var proImages = new Array();

// Retrieve XML document as document object
function loadXMLDoc(url) {
    var req = null;
    try {
        req = new XMLHttpRequest();
        req.overrideMimeType("text/xml");
    }
    catch(e) {
        req = null;
    }
    if (req) {
        xDoc = null;
        req.open("GET", url, true);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    xDoc = req.responseXML;
                    if (xDoc && typeof xDoc.childNodes != "undefined" && xDoc.childNodes.length == 0) {
                        xDoc = null;
                    }
                    else {
                        eProject = xDoc.getElementsByTagName("project")[0];
                        eProject.id = eProject.getElementsByTagName("id")[0].firstChild.data;
                        getProject(eProject.id);
                    }
                }
            }
        }
        req.send(null);
    }
}



function getProject(id) {
count = xDoc.childNodes.length;
i = 0;
for (i; count; i = i + 1) {
    eProject = xDoc.getElementsByTagName("project")[i];
    eProject.id = eProject.getElementsByTagName("id")[0].firstChild.data;
    if (eProject.id == id) {

        // Remove old numbers
        cImages = document.getElementById("cImages");
        if (cImages.firstChild) {
            while (cImages.firstChild)
            {
                cImages.removeChild(cImages.firstChild);
            }
        }

        // Reset Array
        proImages.length = 0;

        eProject.photos = eProject.getElementsByTagName("photos")[0];
        eProject.phot = eProject.photos.getElementsByTagName("photo");
        pCount = eProject.phot.length;
        i = 0;
        for (i; pCount; i = i + 1) {
            /*
             * THIS IS WHERE THE ERROR IS
             */

            eProject.photo = eProject.phot[i];
            // This is the line of code that generates the error message
            eProject.link = eProject.photo.getElementsByTagName("edit")[0].firstChild.data;
            proImages[i] = eProject.link;
            dynamImg = document.getElementById('dynamImg');
            dynamImg.src = proImages[0];
            dynamImg.setAttribute('onclick', 'getNumImg(1)');
            nButton = document.createElement('button');
            nButtonTxt = document.createTextNode(i + 1);
            nButton.appendChild(nButtonTxt);
            nButton.type = "button";
            nButton.value = i;
            if (nButton.value == '0') {
                nButton.setAttribute('id', 'nButtonSelect');
            }
            nButton.setAttribute('onclick', 'getNumImg(' + i + ')');
            cImages.appendChild(nButton);
        }
        break;
    }
}

}

4

3 回答 3

1

Am I correct that you have two loops one inside another and in both of them you use "i" variable as iterator? shouldn't you use "j" in the second loop?

于 2011-08-05T17:32:09.660 回答
1

我认为您希望在您的 for 循环中使用它for (i; i < pCount; i = i + 1) {(注意i <) - 否则它只是检查 pCount 是否已定义并且将无限期运行。我怀疑当它i大于你在中的元素数量时会出错eProject.phot,此时eProject.photo将是未定义的。

于 2011-08-05T17:38:23.687 回答
-2
var t = quotes[i].getElementsByTagName("t")[0].childNodes[0].nodeValue;
于 2015-12-10T18:29:36.283 回答