[英]Getting “Uncaught TypeError: Cannot read property 'top' of undefined” on javascript
I'm using some code I found at Change url when manually scrolled to an id even scrolling to top? 我使用的是在手动滚动到ID甚至滚动到顶部时在“ 更改网址”中找到的一些代码? .
。
It works as intended, but Chrome's Console is giving me an error message, "Uncaught TypeError: Cannot read property 'top' of undefined", for the line: 它可以按预期工作,但是Chrome的控制台向我显示了一条错误消息:“未捕获的TypeError:无法读取未定义的属性'top'”:
var currentElementTop = $(blocksArr[i]).offset().top;
I'd like to get rid of this error, so what do I need to change/add? 我想摆脱此错误,所以我需要更改/添加什么?
(BTW, I would post this question as a comment on the original post, but for some reason I need a reputation of 50...?) (顺便说一句,我会在原始帖子上发表这个问题作为评论,但由于某种原因,我需要50的声誉...?)
Here's the code in action: 这是运行中的代码:
$(function () { var currentHash = "#"; var blocksArr = $('.anchor'); $(document).scroll(function () { var currentTop = window.pageYOffset/1; for (var i=0; blocksArr.length; i++){ var currentElementTop = $(blocksArr[i]).offset().top; var hash = $(blocksArr[i]).attr('id'); if (currentElementTop < currentTop && currentTop < currentElementTop + $(blocksArr[i]).height() && currentHash!=hash){ if(history.pushState) { history.pushState(null, null, '#'+hash); } else { location.hash = '#'+hash; } currentHash = hash; } } }); });
html, body { height: 100%; min-height: 100%; font-size: 100%; text-shadow:none; margin: 0; } div { min-height: 100%; font-size: 100px; padding: 25px 50px; } .hero { line-height: 30vh; padding: 35vh 10vh; max-height: 100vh; min-height: auto; text-align: center; } .red { background: orangered; color: white; } .white { background: ghostwhite; color: black; } .blue { background: blue; color: white; }
<div id="Hero" class="anchor red hero">Hero</div> <div id="About" class="anchor white">About</div> <div id="What" class="anchor blue">What</div> <div id="Why" class="anchor white">Why</div> <div id="Contact" class="anchor red">Contact</div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Your for statement is wrong. 您的for陈述是错误的。
for (var i=0; blocksArr.length; i++){
So if you loop makes it to the end of the array it keeps going and it will not have an element. 因此,如果循环使它到达数组的末尾,它将继续前进,并且将没有元素。 So when you get to
i = blocksArr.length
, that is when the error occurs. 因此,当您到达
i = blocksArr.length
,即发生错误。 jQuery is doing $(undefined).offset().top
and you get the error. jQuery正在执行
$(undefined).offset().top
,您会收到错误消息。
for (var i=0; i < blocksArr.length; i++){
You could try use each
: 您可以尝试使用
each
:
$('.anchor').each((index, obj) => {
var currentElementTop = $(obj).offset().top;
var hash = $(obj).attr('id');
if (currentElementTop < currentTop && currentTop < currentElementTop + $(obj).height() && currentHash!=hash){
if(history.pushState) {
history.pushState(null, null, '#'+hash);
} else {
location.hash = '#'+hash;
}
currentHash = hash
}
});
This line: 这行:
for (var i=0; blocksArr.length; i++){
should be 应该
for (var i=0; i<blocksArr.length; i++){
You're looping from 0 until truthy (blocksArr.length == true) - which it always is. 您从0循环到真实 (blocksArr.length == true)-一直如此。 You don't get an infinite loop because it crashes out on
您不会遇到无限循环,因为它会崩溃
blocksArr[blocksArr.length]
(as array's are 0 based, this should stop at blocksArr[blocksArr.length-1]
) (由于数组基于0,因此应在
blocksArr[blocksArr.length-1]
处停止)
If you were to catch/ignore this, your code would get stuck in an infinite loop. 如果您要捕获/忽略这一点,您的代码将陷入无限循环。
$(function () { var currentHash = "#"; var blocksArr = $('.anchor'); $(document).scroll(function () { var currentTop = window.pageYOffset/1; for (var i=0; i<blocksArr.length; i++){ var currentElementTop = $(blocksArr[i]).offset().top; var hash = $(blocksArr[i]).attr('id'); if (currentElementTop < currentTop && currentTop < currentElementTop + $(blocksArr[i]).height() && currentHash!=hash){ if(history.pushState) { history.pushState(null, null, '#'+hash); } else { location.hash = '#'+hash; } currentHash = hash; } } }); });
html, body { height: 100%; min-height: 100%; font-size: 100%; text-shadow:none; margin: 0; } div { min-height: 100%; font-size: 100px; padding: 25px 50px; } .hero { line-height: 30vh; padding: 35vh 10vh; max-height: 100vh; min-height: auto; text-align: center; } .red { background: orangered; color: white; } .white { background: ghostwhite; color: black; } .blue { background: blue; color: white; }
<div id="Hero" class="anchor red hero">Hero</div> <div id="About" class="anchor white">About</div> <div id="What" class="anchor blue">What</div> <div id="Why" class="anchor white">Why</div> <div id="Contact" class="anchor red">Contact</div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
You might want to wrap the offending line in a try-catch block. 您可能希望将有问题的行包装在try-catch块中。 If you are breaking on the error (which it doesn't sound like you are), this will prevent that.
如果您要解决该错误(听起来好像不对劲),则可以避免这种情况。 Otherwise, this might give you a little bit more detail on what the error is.
否则,这可能会给您更多有关错误原因的详细信息。
Something along these lines: 遵循以下原则:
try {
var currentElementTop = $(blocksArr[i]).offset().top;
}
catch(err) {
console.log(err.message);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.