未捕获的TypeError: Cannot read property 'top'在引导中单击导航栏时未
Uncaught TypeError: Cannot read property 'top' of undefined while clicking on Navigation bar in bootstrap
单击导航菜单中创建的下拉菜单时,会抛出"Uncaught TypeError: Cannot read property 'top' of undefined"。有谁能帮我找出错误吗?
{以下是HTML代码深厚的软件
//这里有一些HTML代码
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="#section1">Home</a></li>
<li><a href="#section2">Services</a></li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Portfolio<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#section31">Animation</a></li>
<li><a href="#section32">WebSite Design</a></li>
<li><a href="#section33">Android Application</a></li>
</ul>
</li>
<li><a href="#section4">Contact Us</a></li>
</ul>
<ul class="nav navbar-nav navbar-right top-right-padding">
<li><a href="#" data-toggle="modal" data-target="#myModal">
<span class="glyphicon glyphicon-user" ></span> Sign Up</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</div>
</div>
</nav>
//java Script Code
// Add scrollspy to <body>
$('body').scrollspy({target: ".navbar", offset: 50});
// Add smooth scrolling on all links inside the navbar
$("#myNavbar a").on('click', function(event) {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
});
}
我已经添加了代码来检查哈希值,如果它是空的,那么不要做任何事情。
// Store hash
var hash = this.hash;
if (hash.length != 0) {
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
}
相关文章:
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 检测在确认页面导航时单击了哪个按钮
- 引导菜单没有't在导航栏中单击打开
- j查询单击导航不起作用
- 单击导航索引数组时发出双重警报
- Phonegap,如何在单击导航栏按钮时触发函数
- Jquery Mobile单击导航栏上的复选框按钮
- reactJS在转到另一个URL时如何传递状态?(单击导航栏上的“新建”,转到提交表单)
- 单击导航选项卡时分配jQuery功能
- 单击导航项定位链接时如何关闭切换菜单
- 单击导航按钮时,如何停止查询动画que
- 未捕获的TypeError: Cannot read property 'top'在引导中单击导航栏时未
- 如何打开下一个屏幕右键单击导航抽屉在反应原生
- 单击导航addClass/removeClass
- 如何使用selenium web驱动程序单击导航栏
- 单击导航后将模式滚动到顶部
- JavaScript按钮,单击导航到另一个HTML页面
- 将单击导航绑定到使用间隔的更改文本
- 如何防止单击导航栏项后重新加载单页应用程序
- 选项卡下拉菜单在引导保持关闭时,单击导航药丸