Wordpress主题上未捕获的TypeError

时间:2015-10-23 12:44:13

标签: javascript jquery

不幸的是,我在没有支持的情况下购买了Wordpress模板,现在我尝试自己调试它。

我无法解决的一个错误是关于JQuery的递归控制台错误。具体来说,只要我用鼠标滚动它就会触发。

Uncaught TypeError: Cannot read property 'top' of undefined

错误的Javascript代码如下:

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

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

            if( aArray[i].indexOf("#") != -1) {

                var theID = aArray[i].substr(aArray[i].indexOf("#"));
                if (theID.length) {
                    var divPos = jQuery(theID).offset().top; // HERE IS THE ERROR, get the offset of the div from the top of page
                    var divHeight = jQuery(theID).height(); // get the height of the div in question
                    if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
                        if(jQuery("a[href='" + theID + "']").length) {
                            jQuery("a[href='" + theID + "']").addClass("nav-active");
                        }
                        else if(jQuery("a[href='" + aArray[i] + "']").length) {
                            jQuery("a[href='" + aArray[i] + "']").addClass("nav-active");
                        }
                    } else {
                        if(jQuery("a[href='" + theID + "']").length) {
                            jQuery("a[href='" + theID + "']").removeClass("nav-active");
                        }
                        else if(jQuery("a[href='" + aArray[i] + "']").length) {
                            jQuery("a[href='" + aArray[i] + "']").removeClass("nav-active");
                        }   
                    }
                }
            }   
        }

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

错误会在var divPos = jQuery(theID).offset().top;行上触发。我手动添加if statement来检查是否有theID变量,然后执行代码,但仍然会在浏览器控制台上触发错误。

1 个答案:

答案 0 :(得分:0)

如果脚本无法在DOM中找到该对象,它将会中断。尝试检查它是否存在。

if (theID.length) {
        // Check if you find the object in the DOM
        if(jQuery(theID).length > 0) {
                    var divPos = jQuery(theID).offset().top; // HERE IS THE ERROR, get the offset of the div from the top of page
                    var divHeight = jQuery(theID).height(); // get the height of the div in question
                    if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
                        if(jQuery("a[href='" + theID + "']").length) {
                            jQuery("a[href='" + theID + "']").addClass("nav-active");
                        }
                        else if(jQuery("a[href='" + aArray[i] + "']").length) {
                            jQuery("a[href='" + aArray[i] + "']").addClass("nav-active");
                        }
                    } else {
                        if(jQuery("a[href='" + theID + "']").length) {
                            jQuery("a[href='" + theID + "']").removeClass("nav-active");
                        }
                        else if(jQuery("a[href='" + aArray[i] + "']").length) {
                            jQuery("a[href='" + aArray[i] + "']").removeClass("nav-active");
                        }   
                    }
                }
           }