如果取消注释 body.children().each(calc_deep(1));
然后我得到 TypeError: Object [object Window] has no method 'attr'
parseInt(deepest.attr('deep'))
字符串,但无需取消注释,您可以在控制台中检查是否可以调用 deepest.attr('deep')
。那是什么?
var deepest;
var calc_deep = function(i)
{
$(this).attr('deep',i);
if(i>parseInt(deepest.attr('deep')))
deepest=this;
$(this).children().each(calc_deep(i+1));
}
var find_deepest = function()
{
body=$('body').children().eq(0);
body.attr('deep',0);
deepest=body;
//body.children().each(calc_deep(1));
}
find_deepest();
最佳答案
each
接受一个函数作为参数,您将其传递给 undefined
- 因为您首先调用该函数,然后它的返回值是 each( )
获取。
改用function() {calc_deep(1);}
。
关于JavaScript 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11659851/