无法调用未定义的方法

无法调用未定义的方法

本文介绍了Uncaught TypeError:无法调用未定义的方法'hasChildNodes'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在jQuery中使用ajax请求读取XML。下面是代码,它在IE中工作正常,但是当我在Chrome上运行时,我面临这个错误。
$ b



  $。ajax({

键入:GET,
url:tree1.xml,
dataType:xml,
成功:函数(xml){$ b $ root = xml.documentElement;
childs = root.childNodes;
for(var i = 0; i< childs.length; i ++){
z = childs [i] .childNodes;
adChilds(z,childs ,oNode);


函数adChilds(a,b,c){
if(a [i] .hasChildNodes()){
adNode(b [ i] .nodeName,c);
var oNode_ch = oNode1;
for(var j = 0; j child1 = a [j] .childNodes;
adNode(child1 [0] .nodeValue,oNode_ch);
}
}
else {adNode(a [0] .nodeValue,oNode);}
}
}

错误指向此行。



if(a [i] .hasChildNodes()){



任何人都可以告诉我我错在哪里。

预先致谢!

解决方案

您正在做这件事:

  z =孩子的[I] .childNodes; 

然后,在 adChilds(z,childs,oNode)的第一行中, ; ,你这样做:

  if(a [i] .hasChildNodes()) {

但是, i 不是索引到 a 的子元素中。这是一个儿童父母的索引。因此,如果 a 的父项的子项数量与 a 的子项数量相同,我没有确切知道你在adChilds()中想要完成什么,所以我不确定什么修复建议,但我假设如果你想处理 a 的子女,你应该得到 a 的子女人数,并且让确保您只能访问实际存在的号码。




一些编码建议:


  1. 我强烈建议您使用真实的变量名称。名称如 a b c 和<$ c对于中间变量来说,$ c> z 是含糊不清的,并且让你的代码难以阅读。

  2. code> var 在第一次定义时(或者定义在函数的顶部),否则它们会变成全局变量,这就要求麻烦了,尤其是使用异步回调函数。


  3. 当您在首次检查代码时发现对您不明显的错误时,请在您最喜欢的调试器中设置一个断点,然后遍历代码并检查变量的状态确切地看你为什么会得到错误。如果您不知道如何使用调试器,请学习。它们内置于大多数浏览器中,对于高效调试非常简单且绝对必不可少。

  4. I am trying to read a XML using ajax request in jquery. Below is the code,it's working fine in IE but when I run this on Chrome i am facing this error

    $.ajax({
    
        type: "GET",
        url: "tree1.xml",
        dataType:"xml",
        success: function(xml){
            root=xml.documentElement;
            childs=root.childNodes;
            for(var i=0;i<childs.length;i++){
                z=childs[i].childNodes;
                adChilds(z,childs,oNode);
    
    
                function adChilds(a,b,c){
                    if(a[i].hasChildNodes()){
                        adNode(b[i].nodeName,c);
                        var oNode_ch=oNode1;
                        for(var j=0;j<a.length;j++){
                            child1=a[j].childNodes;
                            adNode(child1[0].nodeValue,oNode_ch);
                        }
                    }
                    else{adNode(a[0].nodeValue,oNode);}
                }
            }
    

    error is pointing at this line.

    if(a[i].hasChildNodes()){

    Can anyone suggest me where am I going wrong.

    Thanks in advance!

    解决方案

    You're doing this:

    z=childs[i].childNodes;
    

    then, in the first line of adChilds(z,childs,oNode);, you're doing this:

    if(a[i].hasChildNodes()){
    

    But, i isn't an index into the children of a. It's an index into the parent's of a's children. Thus, if the parent of a doesn't have the same number of children as a has children, you will go out of index.

    I don't know exactly what you're trying to accomplish in adChilds() so I'm not sure what fix to suggest, but I presume you that if you want to deal with the children of a, you should get the number of children of a and make sure you only access the number that actually exist


    Some coding suggestions:

    1. I'd strongly suggest you use real variable names. Names like a, b, c and z for intermediate variables are cryptic and make your code hard to read.

    2. All local variables should be preceded with var at first definition (or defined at the top of the function), otherwise they become global variables which is asking for trouble, especially with asynchronous callback functions.

    3. When you see errors that aren't obvious to you upon first inspection of your code, then set a breakpoint in your favorite debugger and step through the code and examine the state of the variables to see exactly why you're getting the error. If you don't know how to use a debugger, learn. They are built into most browsers, easy and absolutely essential to efficient debugging.

    这篇关于Uncaught TypeError:无法调用未定义的方法'hasChildNodes'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:09