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

Uncaught TypeError: Cannot read property 'top' of undefined

本文关键字:未定义 读取 属性 top 类型 错误      更新时间:2023-09-26

我有一些这样的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);
    });
});

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

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

我正在使用从谷歌 API 加载的 jQuery 1.8.3。

如果the_id是 id,那么你需要

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