Uncaught TypeError: Cannot read property 'top' undefined

Bekam ich eine Fehlermeldung wie z.B. "Uncaught TypeError: Cannot read property 'top' of undefined "... ich weiß nicht, die Art und Weise zu lösen!!.. kann mir jemand sagen, die Antwort!!!..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");
            }
        }
    });
Es ist offensichtlich,... theID keine ID des Elements auf der Seite, so dass Ihre Selektor gibt nichts zurück, und Sie können nicht nennen top von undefined

InformationsquelleAutor Thirumani guhan | 2014-08-12

Schreibe einen Kommentar