2013-01-23 85 views
3

财产“顶”我有一些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。

+2

这个问题很可能已经回答看看[控制台](https://developers.google.com/chrome-developer-tools /文档/控制台)。 –

+1

我的猜测是你有一个散列,你砍掉了“#”,你真的需要它。但是因为你没有显示这个链接是什么样子,所以这是一个猜测。 – epascarello

回答

9

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

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