gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'top' of undefined

转载 作者:太空宇宙 更新时间:2023-11-03 17:53:38 26 4
gpt4 key购买 nike

我不断收到此错误:“未捕获类型错误:无法读取未定义的属性‘top’”。有谁知道我做错了什么?我有 2 个具有不同值的相同循环,但它们不会执行,第一个循环会执行但其余循环不会执行。我是 jQuery 的新手,所以如果有人能告诉我我做错了什么,那就太好了。

for(i=4, x=1; x<16; i=i+3, x=x+3)
{
var top = $('.box-' + x).position().top + $('.box-' + x).height() + 10;
var left = $('.box-' + x).position().left;

$('.box-' + i).css({
top: top,
left: left
});
}

我不断收到此错误:“未捕获类型错误:无法读取未定义的属性‘top’”。

最佳答案

正如@dfsq 所指出的,某些目标元素可能在页面上不可用。所以下面的代码将只对那些操作:

for(i=4, x=1; x<16; i=i+3, x=x+3)
{
if( $('.box-' + x).length ) {
var top = $('.box-' + x).position().top + $('.box-' + x).height() + 10;
var left = $('.box-' + x).position().left;

$('.box-' + i).css({
top: top,
left: left
});
}
}

关于javascript - 类型错误 : Cannot read property 'top' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26739510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com