文档节点返回null [英] Document nodes returning null
问题描述
我正在编写一些Javascript代码,要求我循环某个父节点的所有子节点和子子节点,并使用相关子节点执行一个函数。
我决定编写一个带有所述功能的递归函数,一个节点和一个函数作为参数,但它根本不起作用。
我的问题是每当我将一个节点传递给这个函数参数时无论我改变什么,总是等于null。我甚至修改了我的自定义传递函数来忽略空节点,现在每次我的函数递归时它都抛出一个空变量错误。
(确切错误:node.childNodes没有属性)
对我来说,这听起来像是一个臭名昭着的封闭问题。我理解这个概念,但由于缺乏经验,我不确切知道它们在这种情况下是如何工作的:(。
我知道有很多样本代码可以做这项工作,但我想知道为什么这个看起来完全合乎逻辑的功能,并没有像我期望的那样表现。
我在这里和那里阅读了一些相关的帖子这个,我相应地修改了我的代码,但我似乎无法弄清楚是什么导致了我的问题。
嗨...
欢迎来到TSDN ...
i修复你的代码...有一个仔细看看这个例子...我在firebug中用google.com测试了它并且它有效:
展开 | 选择 | 换行 | 行号
< blockquote>嘿......好吧我有点尴尬:\
thx为你的帮助XD
请原谅 - 我错过了gits post。
展开 | 选择 | Wrap | 行号
Im writing some Javascript code that requires me to loop all child nodes and sub child nodes of a certain parent node and execute a function with the child node in question.
I decided to write a recursive function with said functionality, with a node and a function as parameters, but it simply wont work.
My problem is that whenever i pass a node to this function parameter it is always, regardless of what i change, equal to null. I even modified my custom passed function to ignore null nodes, and now it throw a null variable error every time my function recurses.
(exact error: node.childNodes has no properties)
To me it looks like a problem with these infamous ''closures'' i keep hearing about. I understand the concept, but I dont know exactly how they work in this context due to a great lack of experience :(.
I know there are many sample codes out there that can do the job, but I want to know why THIS function, which looks perfectly logical to me, doesnt behave as i would expect it too.
I read a few posts here and there relating to this, and I modified my code accordingly, but i cant seem to figure out just what is causing my problem.
Expand|Select|Wrap|Line Numbers解决方案hi ...
welcome to TSDN ...
i fixed your code ... have a close look at the example ... i tested it with google.com in firebug and it worked:
Expand|Select|Wrap|Line Numbers
heh... well im kinda embarrassed :\
thx for your help XD
Excuse this- I missed gits post.
Expand|Select|Wrap|Line Numbers
这篇关于文档节点返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文登录 关闭
扫码关注1秒登录发送“验证码”获取 | 15天全站免登陆