gpt4 book ai didi

jquery - 未捕获的类型错误 : Cannot read property 'top' of undefined

转载 作者:行者123 更新时间:2023-12-01 07:55:02 24 4
gpt4 key购买 nike

我收到一个错误,例如“未捕获的类型错误:无法读取未定义的属性“顶部””...我不知道如何修复!!...任何人都可以告诉我答案!!!...var divPos = $(theID).offset().top;

var aChildren = $("nav li").children(); // find the a children of the list items
var aArray = []; // create the empty aArray
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('href');
aArray.push(ahref);
} // this for loop fills the aArray with attribute href values

$(window).scroll(function(){
var windowPos = $(window).scrollTop()+85; // get the offset of the window from the top of page
var windowHeight = $(window).height(); // get the height of the window
var docHeight = $(document).height();

for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];



var divPos = $(theID).offset().top;// get the offset of the div from the top of page.... error occured here!!!




var divHeight = $(theID).height(); // get the height of the div in question
if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
$("a[href='" + theID + "']").addClass("active");
} else {
$("a[href='" + theID + "']").removeClass("active");
}
}

if(windowPos + windowHeight == docHeight) {
if (!$("nav li:last-child a").hasClass("active")) {
var navActiveCurrent = $(".active").attr("href");
$("a[href='" + navActiveCurrent + "']").removeClass("active");
$("nav li:last-child a").addClass("active");
}
}
});

最佳答案

我刚刚更改了

var divPos = $(theID).offset().top;

var divPosid = $(theID);
if (!divPosid.length) {
return;
}
var divPos = divPosid.offset().top; // get the offset of the div from the top of page

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

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