未定义的偏移但id似乎有效

时间:2015-07-08 13:46:46

标签: jquery

我有一个脚本滚动到页面上的锚点,它有点工作(虽然不是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

0 个答案:

没有答案