简体   繁体   English

未捕获的类型错误:无法读取未定义的属性“顶部”

[英]Uncaught TypeError: Cannot read property 'top' of undefined

I apologize if this question has already been answered.如果这个问题已经得到回答,我深表歉意。 I've tried searching for solutions but could not find any that suited my code.我试过寻找解决方案,但找不到任何适合我的代码的解决方案。 I'm still new to jQuery.我还是 jQuery 的新手。

I have two different types of sticky menus for two different pages.对于两个不同的页面,我有两种不同类型的粘性菜单。 Here's the code for both.这是两者的代码。

$(document).ready(function () {
    var contentNav = $('.content-nav').offset().top;
    var stickyNav = function () {
        var scrollTop = $(window).scrollTop();
        if (scrollTop > contentNav) {
            $('.content-nav').addClass('content-nav-sticky');
        } else {;
            $('.content-nav').removeClass('content-nav-sticky')
        }
    };
    stickyNav();
    $(window).scroll(function () {
        stickyNav();
    });
});
$(document).ready(function () {
    var stickyNavTop = $('.nav-map').offset().top;
    // var contentNav = $('.content-nav').offset().top;
    var stickyNav = function () {
        var scrollTop = $(window).scrollTop();
        if (scrollTop > stickyNavTop) {
            $('.nav-map').addClass('sticky');
            // $('.content-nav').addClass('sticky');
        } else {
            $('.nav-map').removeClass('sticky');
            // $('.content-nav').removeClass('sticky')
        }
    };
    stickyNav();
    $(window).scroll(function () {
        stickyNav();
    });
});

My problem is that the code for the sticky side menu on the bottom doesn't work because the second line of code var contentNav = $('.content-nav').offset().top;我的问题是底部粘性侧边菜单的代码不起作用,因为第二行代码var contentNav = $('.content-nav').offset().top; fires a error that reads "Uncaught TypeError: Cannot read property 'top' of undefined".触发一个错误,内容为“未捕获的类型错误:无法读取未定义的属性 'top'”。 In fact, no other jQuery code below that second line works at all unless they are placed above it.事实上,第二行下面的其他 jQuery 代码根本无法工作,除非它们被放置在它上面。

After some researching, I think the problem is that $('.content-nav').offset().top can't find the specified selector because it's on a different page.经过一番研究,我认为问题在于$('.content-nav').offset().top找不到指定的选择器,因为它位于不同的页面上。 If so, I can't find a solution.如果是这样,我找不到解决方案。

Check if the jQuery object contains any element before you try to get its offset:在尝试获取其偏移量之前,请检查 jQuery 对象是否包含任何元素:

var nav = $('.content-nav');
if (nav.length) {
  var contentNav = nav.offset().top;
  ...continue to set up the menu
}

Your document does not contain any element with class content-nav , thus the method .offset() returns undefined which indeed has no top property.您的文档不包含任何具有 class content-nav元素,因此方法.offset()返回undefined ,它确实没有top属性。

You can see for yourself in this fiddle你可以在这个小提琴中亲眼看到

alert($('.content-nav').offset());

(you will see "undefined") (你会看到“未定义”)

To avoid crashing the whole code, you can have such code instead:为了避免整个代码崩溃,您可以使用这样的代码:

var top = ($('.content-nav').offset() || { "top": NaN }).top;
if (isNaN(top)) {
    alert("something is wrong, no top");
} else {
    alert(top);
}

Updated fiddle .更新了小提琴

The problem you are most likely having is that there is a link somewhere in the page to an anchor that does not exist.您最有可能遇到的问题是页面中某处存在指向不存在的锚点的链接。 For instance, let's say you have the following:例如,假设您有以下内容:

<a href="#examples">Skip to examples</a>

There has to be an element in the page with that id, example:页面中必须有一个具有该 ID 的元素,例如:

<div id="examples">Here are the examples</div>

So make sure that each one of the links are matched inside the page with it's corresponding anchor.因此,请确保页面内的每个链接都与其对应的锚点相匹配。

I know this is extremely old, but I understand that this error type is a common mistake for beginners to make since most beginners will call their functions upon their header element being loaded.我知道这是非常古老的,但我知道这种错误类型是初学者经常犯的错误,因为大多数初学者会在加载标题元素时调用他们的函数。 Seeing as this solution is not addressed at all in this thread, I'll add it.鉴于此线程中根本没有解决此解决方案,我将添加它。 It is very likely that this javascript function was placed before the actual html was loaded .这个 javascript 函数很可能是在加载实际 html 之前放置的 Remember, if you immediately call your javascript before the document is ready then elements requiring an element from the document might find an undefined value.请记住,如果您在文档准备好之前立即调用您的 javascript,那么需要来自文档的元素的元素可能会找到未定义的值。

I ran through similar problem and found that I was trying to get the offset of footer but I was loading my script inside a div before the footer.我遇到了类似的问题,发现我试图获取页脚的偏移量,但我在页脚之前将脚本加载到 div 中。 It was something like this:它是这样的:

<div> I have some contents </div>
<script>
  $('footer').offset().top;
</script>
<footer>This is footer</footer>

So, the problem was, I was calling the footer element before the footer was loaded.所以,问题是,我在页脚加载之前调用了页脚元素。

I pushed down my script below footer and it worked fine!将我的脚本推到页脚下方,效果很好!

If this error appears whenever you click on the <a> tag.如果每次单击<a>标签时都会出现此错误。 Try the code below.试试下面的代码。

CORRECT :正确:

<a href="javascript:void(0)">

This malfunction occurs because your href is set to # inside of your <a> tag.出现此故障是因为您的 href 设置为<a>标签内的 #。 Basically, your app is trying to find href which does not exist.基本上,您的应用正在尝试查找不存在的 href。 The right way to set empty href is shown above.设置空 href 的正确方法如上所示。

WRONG :错误:

<a href="#">

I had the same problem ("Uncaught TypeError: Cannot read property 'top' of undefined")我遇到了同样的问题(“未捕获的类型错误:无法读取未定义的属性 'top'”)

I tried every solution I could find and noting helped.我尝试了我能找到的所有解决方案,并注意到有帮助。 But then I've spotted that my DIV had two IDs.但是后来我发现我的 DIV 有两个 ID。

So, I removed second ID and it worked.所以,我删除了第二个 ID 并且它起作用了。

I just wish somebody told me to check my IDs earlier))我只是希望有人告诉我早点检查我的身份证))

In my case, I found a strange issue, I use this function to autoscroll to the last comment when posted by users, Actually, I added the same function twice in separate pages, when activate it for one page and disable it in the other the issue occur, when activate it in both pages the function passed successfully.就我而言,我发现了一个奇怪的问题,当用户发布时,我使用此功能自动滚动到最后一条评论,实际上,我在不同的页面中添加了两次相同的功能,当为一个页面激活它并在另一个页面禁用它时出现问题,当在两个页面中激活它时,功能成功通过。

So strange :(好奇怪:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法读取未定义的属性“顶部” - Uncaught TypeError: Cannot read property ‘top’ of undefined 未捕获的TypeError:无法读取未定义的属性“ top” - Uncaught TypeError: Cannot read property 'top' of undefined 未捕获的TypeError:无法读取未定义的属性“ top” - Uncaught TypeError: Cannot read property 'top' of undefined 未捕获的TypeError:无法读取未定义的属性“ top” - Uncaught TypeError: Cannot read property 'top' of undefined 购物车飞未捕获的TypeError:无法读取未定义的属性“顶部” - Cart to Fly Uncaught TypeError: Cannot read property 'top' of undefined 图表 JS 错误:未捕获的类型错误:无法读取未定义的属性“顶部” - Chart JS Error : Uncaught TypeError: Cannot read property 'top' of undefined jQuery Uncaught TypeError:无法读取未定义的属性“ top” - jQuery Uncaught TypeError: Cannot read property 'top' of undefined Uncaught TypeError:无法读取未定义的属性“ top”-滚动错误? - Uncaught TypeError: Cannot read property 'top' of undefined - Scrolling error? 未捕获的TypeError:无法读取未定义的属性“ top”(jQuery) - Uncaught TypeError: Cannot read property 'top' of undefined (jquery) 控制台错误:未捕获TypeError:无法读取未定义的属性“顶部” - Console Error: Uncaught TypeError: Cannot read property 'top' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM