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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14489768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:59:04  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'top' of undefined

javascriptjquery

提问by Giri

I have some jQuery code like this

我有一些像这样的 jQuery 代码

$(document).ready(function(){
    $('.content-nav a').on('click',function(){
      var str = $(this).attr("href");   
      var the_id = str.substr(1);
        $("#container").animate({ scrollTop: $(the_id).offset().top }, 1000);
    });
});

When I click the link i'm getting error like Uncaught TypeError: Cannot read property 'top' of undefined

当我点击链接时,我收到了类似的错误 Uncaught TypeError: Cannot read property 'top' of undefined

Can someone tell me whats wrong?

有人可以告诉我出了什么问题吗?

I'm using jQuery 1.8.3 which is loaded from google api.

我正在使用从 google api 加载的 jQuery 1.8.3。

回答by Denys Séguret

If the_idis an id, then you need

如果the_id是一个id,那么你需要

$('#'+the_id).offset().top