未捕获的TypeError:无法读取未定义的属性"top"

Gir*_*iri 3 javascript jquery

我有一些像这样的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);
    });
});
Run Code Online (Sandbox Code Playgroud)

当我点击链接时,我得到的错误就像 Uncaught TypeError: Cannot read property 'top' of undefined

谁能告诉我什么是错的?

我正在使用从谷歌api加载的jQuery 1.8.3.

Den*_*ret 9

如果the_id是id,那么你需要

$('#'+the_id).offset().top
Run Code Online (Sandbox Code Playgroud)