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

时间:2013-01-23 21:31:35

标签: 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);
    });
});

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

有人能告诉我什么错了吗?

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

1 个答案:

答案 0 :(得分:9)

如果the_id是ID,则需要

$('#'+the_id).offset().top
相关问题