无法读取未定义的链接 href 的属性“top”
Cannot read property 'top' of undefined of link href
我正在通过滚动为导航中的链接创建一个活动类。
function onScroll(event){
var scrollPosition = $(document).scrollTop();
$('.sticky-navigation-posts a').each(function () {
var currentLink = $(this);
var refElement = $(currentLink.attr("href"));
console.log(refElement);
if (refElement.position().top <= scrollPosition && refElement.position().top + refElement.height() > scrollPosition) {
$('.sticky-navigation-posts .title-wrapper .post-title a').removeClass("active");
console.log('yes!');
currentLink.addClass("active");
} else {
currentLink.removeClass("active");
console.log('no!');
}
});
}
当我console.log(refElement)
时,该元素被记录下来。但是如果我console.log(refElement.position().top)
,则此错误将抛Cannot read property 'top' of undefined
。我已经调试了一段时间了,但无法弄清楚。有什么想法吗?我也尝试过将refElement
包裹在$()
中。
所有这些代码都是包装在$(document).ready()
函数中。
好的,
我现在明白你的意思了。但是,它对我来说工作正常。不确定您在哪里遇到问题?
我添加了console.log(refElement.position().top);
,它正在正确记录到控制台,甚至所需的行为也在工作!? http://jsfiddle.net/thesane/Dxtyu/3133/
相关文章:
- jQuery控制台错误:无法读取属性'top'的未定义
- Jquery scrollTop动画不工作"无法读取属性'top'无风
- 使用javascript动态计算top属性
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 正在尝试滚动页面:未捕获类型错误:无法读取属性'top'的未定义
- IE7/8未添加css属性'top'动态地作为内联样式
- 未捕获的类型错误:无法读取属性'top'的未定义
- 未捕获的类型错误:无法读取未定义的属性“top”
- 为什么我会收到错误:未捕获的类型错误:无法使用此代码读取 null 的属性“top”
- JavaScript 运行时错误:无法获取未定义或空引用的属性“top”
- 无法读取未定义的链接 href 的属性“top”
- 未捕获的类型错误:无法读取未定义错误的属性“top”
- 偏移 JavaScript 滚动:无法读取未定义错误的属性“top”
- 如何根据php标题的长度调整margin-top属性
- TypeError:无法读取属性'top'的未定义
- 无法读取属性'top'未定义的Jquery/javascript
- Drupal主题js错误-无法读取属性'top'的定义
- 未捕获的类型错误:无法读取未定义的属性“top”
- jQuery uncatch TypeError: 无法读取 undefined 的属性 'top'
- 未捕获的类型错误:不能设置属性'top'的定义