gpt4 book ai didi

javascript - 无法读取链接 href 未定义的属性 'top'

转载 作者:行者123 更新时间:2023-12-03 07:37:08 25 4
gpt4 key购买 nike

我正在通过滚动为导航中的链接建立一个事件类。

    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),则会抛出此错误无法读取未定义的属性“top”。我已经调试了一段时间了,无法弄清楚。有任何想法吗?我还尝试将 refElement 包装在 $() 中。
所有这些代码都包装在 $(document).ready() 中 函数。

最佳答案

好吧,我现在明白你的意思了。然而,它对我来说工作得很好。不知道哪里出了问题?

我添加了 console.log(refElement.position().top); 并且它正确记录到控制台,甚至所需的行为也正常工作!? http://jsfiddle.net/thesane/Dxtyu/3133/

关于javascript - 无法读取链接 href 未定义的属性 'top',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35562856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com