[英]Error: Uncaught TypeError: Cannot read property 'top' of undefined
I have this code 我有这个代码
jQuery(function($) {
'use strict';
$(window).scroll(function(event) {
Scroll();
});
$('.navbar-collapse ul li a').on('click', function() {
$('html, body').animate({
scrollTop: $(this.hash).offset().top - 5
}, 1000);
return false;
});
function Scroll() {
var contentTop = [];
var contentBottom = [];
var winTop = $(window).scrollTop();
var rangeTop = 200;
var rangeBottom = 500;
$('.navbar-collapse').find('.scroll a').each(function() {
contentTop.push($($(this).attr('href')).offset().top);
contentBottom.push($($(this).attr('href')).offset().top + $($(this).attr('href')).height());
})
$.each(contentTop, function(i) {
if (winTop > contentTop[i] - rangeTop) {
$('.navbar-collapse li.scroll')
.removeClass('active')
.eq(i).addClass('active');
}
})
};
I tried keep getting this error when loading the code: 我尝试在加载代码时不断收到此错误:
Uncaught TypeError: Cannot read property 'top' of undefined. Uncaught TypeError:无法读取未定义的属性“ top”。 The effected lines are 9 and 21
受影响的行是9和21
Just in case this helps, the site this code is loading on works fine as it's html file, but doesn't when its set up as a WHMCS theme The HTML this should be working with is 以防万一这有帮助,此代码正在加载的站点可以正常工作,因为它是html文件,但当将其设置为WHMCS主题时,该站点就不能正常工作
<nav id="main-nav" class="navbar navbar-default navbar-fixed-top" role="banner">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php"><img src="templates/six/images/logo.png" alt="logo"></a>
</div>
<div class="collapse navbar-collapse navbar-right">
<ul class="nav navbar-nav">
<li class="scroll active"><a href="#home" class="active">Home</a></li>
<li class="scroll"><a href="#features">Features</a></li>
<li class="scroll"><a href="#services">Services</a></li>
<li class="scroll"><a href="#domains">Domains</a></li>
<li class="scroll"><a href="#pricing">Pricing</a></li>
<li class="scroll"><a href="#clientarea">Client Area</a></li>
<li class="scroll"><a href="#contact-us">Support</a></li>
</ul>
</div>
</div><!--/.container-->
</nav>
The problem is this: 问题是这样的:
$($(this).attr('href')).offset().top
You are sending the result of getting the href into the Jquery $
function which will return an object with an undefined offset()
as it's not part of the page. 您正在发送将href放入Jquery
$
函数的结果,该函数将返回带有未定义offset()
的对象,因为该对象不属于页面。
$('my-a-tag').offset().top
where my-a-tag
finds your a tag uniquely will work, as long as my-a-tag
exists on the page. $('my-a-tag').offset().top
在那里my-a-tag
就会发现你的标签唯一会的工作,只要my-a-tag
页面上存在。
This jsfiddle here demonstrates. 这个jsfiddle在这里演示。 Open the console to see the difference between the object returned by your code and the object returned by the correct code.
打开控制台以查看代码返回的对象与正确代码返回的对象之间的区别。 Also, see the
offset()
function working correctly. 另外,请参见
offset()
函数正常工作。
Here's also an example line that should work in your code: 这也是应该在您的代码中运行的示例行:
contentTop.push($(this).offset().top);
In this this case we just use this
rather than a class like in the jsfiddle as we're in a loop which is going through all the anchors. 在这种情况下,我们只使用
this
而不是像jsfiddle中那样的类,因为我们处于循环通过所有锚点的循环中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.