[英]Clicking on anchor link returns NULL as offset
I have a standard menu and JS for smooth scrolling between the anchors. 我有一个标准菜单和JS,可在锚点之间平滑滚动。 When I click on one of the anchors it do nothing but i get an error in the dev tools.
当我单击某个锚点时,它什么都不做,但是在开发工具中出现错误。
Here is my menu: 这是我的菜单:
<div class="menu anc">
<ul class="nav menu">
<li class="item-111"><a href="#stills">stills</a></li>
<li class="item-112"><a href="#art">art</a></li>
<li class="item-113"><a href="#interactive">interactive</a></li>
<li class="item-114"><a href="#about">about</a></li>
<li class="item-115"><a href="#contact">contact</a></li>
</ul>
</div>
The JS part: JS部分:
$( document ).ready(function() {
$("header ul.menu li:first-child").addClass('active');
$(".anc a").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});
});
http://jsfiddle.net/amitt314/tXaLC/5/ http://jsfiddle.net/amitt314/tXaLC/5/
and the error from crome dev tools: 以及来自crome dev工具的错误:
> Uncaught TypeError: Cannot call method 'offset' of null script.js:18
> (anonymous function) script.js:18 x.event.dispatch jquery.js:4676
> y.handle jquery.js:4360
Why The anchor returns a NULL value? 为什么定位符返回NULL值? when i trying to remove the script everything works just fine.
当我尝试删除脚本时,一切正常。
$( document ).ready(function() {
$("header ul.menu li:first-child").addClass('active');
$(".anc a").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:$(event.target.hash).offset().top}, 500);
});
});
Updated you have to include jquery
first then try $($(this).attr('href'))
in place of $(this.hash)
like , 更新后,您必须先包含
jquery
然后尝试使用$($(this).attr('href'))
代替$(this.hash)
例如,
$(".anc a").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:$($(this).attr('href')).offset().top}, 500);
});
I found the problem. 我发现了问题。 It was a jQuery conflict.
这是一个jQuery冲突。
Probably one of the components loaded jQuery to my page and cause this conflict. 可能其中一个组件将jQuery加载到了我的页面上并导致此冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.