我有一个脚本滚动到页面上的锚点,它有点工作(虽然不是100%),我在控制台中不断遇到以下问题:
TypeError: $(...).offset(...) is null
var divPos = $(theID).offset().top;
但是,如果我发出警告theID
,我会得到一个正确的href,例如#l1
但它一直表现得好像不能完全识别它。
相关代码如下:
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(); // 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
var divHeight = $(theID).height(); // get the height of the div in question