未捕获的TypeError: Cannot read property 'top'的定义
Uncaught TypeError: Cannot read property 'top' of undefined
对不起,我的英语不好,但是我有一个问题。
这是我的留言:
Uncaught TypeError: Cannot read property 'top' of undefined
下面是我的函数:
function onScroll(event){
var scrollPos = $(document).scrollTop();
$('#menu-center a').each(function () {
var currLink = $(this);
var refElement = $(currLink.attr("href"));
if (refElement.position().top<= scrollPos && refElement.position().top + refElement.heig
ht() > scrollPos) {
$('#menu-center ul li a').removeClass("active");
currLink.addClass("active");
}
else{
currLink.removeClass("active");
}
});
}
我不明白,因为我在本地有另一个页面使用这个函数,它工作得很好。
我在网上搜索了,但没有任何符合我问题的。
谢谢你的帮助
改变这一行:var refElement = $(currLink.attr("href"));
var refElement = $(currLink).attr("href");
我找到了解决上述问题的方法。参见下面的代码片段
jQuery(document).ready(function($) {
$(document).on("scroll", onScroll);
});
function onScroll(event){
var scrollPos = $(document).scrollTop();
$('#menu-center a').each(function () {
var currLink = $(this);
var refElement = currLink.attr("href");
var splitEle = refElement.split('#');
if ($("#"+splitEle[1]).offset().top <= scrollPos && $("#"+splitEle[1]).offset().top + $("#"+splitEle[1]).height() > scrollPos) {
$('#menu-center ul li').removeClass("active");
currLink.parent().addClass("active");
}else{
currLink.parent().removeClass("active");
}
});
}
相关文章:
- jQuery控制台错误:无法读取属性'top'的未定义
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 正在尝试滚动页面:未捕获类型错误:无法读取属性'top'的未定义
- 未捕获的类型错误:无法读取属性'top'的未定义
- 重定义对最顶层窗口的引用 (window.top)
- 未捕获的类型错误:无法读取未定义的属性“top”
- JavaScript 运行时错误:无法获取未定义或空引用的属性“top”
- 无法读取未定义的链接 href 的属性“top”
- 未捕获的类型错误:无法读取未定义错误的属性“top”
- 偏移 JavaScript 滚动:无法读取未定义错误的属性“top”
- TypeError:无法读取属性'top'的未定义
- 无法读取属性'top'未定义的Jquery/javascript
- 在iframe中定义top
- 未捕获的类型错误:无法读取属性'top'的未定义(popupWindow.top.close();)
- 控制台错误:未捕获类型错误:无法读取属性'top'的未定义
- 未捕获的TypeError: Cannot read property 'top'的定义
- 不能读取property 'top'未定义的Ruby on Rails项目
- JavaScript中的Top或parent在使用FireFox或Opera的SharePoint 2010中未定义
- 未捕获的TypeError: Cannot read property 'top'的定义
- Drupal主题js错误-无法读取属性'top'的定义