Bootstrap Scrollspy Uncaught TypeError:无法读取未定义的属性“top”

时间:2015-08-08 02:56:57

标签: javascript jquery twitter-bootstrap

Uncaught TypeError: Cannot read property 'top' of undefined

我的代码

$("#navbar-collapse ul li a[href^='#']").on('click', function(e) {

   // store hash
   var hash = this.hash;

   // prevent default anchor click behavior
   e.preventDefault();

   // animate
   $('html, body').animate({
       scrollTop: $(hash).offset().top
     }, 300, function(){

       // when done, add hash to url
       // (default click behaviour)
       window.location.hash = hash;
     });

});

这是我的标题html结构

<header class="main-header">
<nav class="navbar navbar-static-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <a href="/" class="navbar-brand">Testing</a>
            <button type="button" class="navbar-toggle collapsed" data-target="#navbar-collapse">
                <i class="fa fa-bars"></i>
            </button>
        </div>

        <div class="collapse navbar-collapse" id="navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Scrollspy Testing</a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#">Testing</a></li>
                        <li><a href="#">Testing</a></li>
                        <li class="divider"></li>
                        <li><a href="#">See all products</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Testing Info</a>
                    <ul class="dropdown-menu">
                        <li><a href="#blog">Blog</a></li>
                        <li><a href="#about">About Us</a></li>
                        <li><a href="#contact">Contact Us</a></li>
                        <li><a href="#support">Support</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

任何想法我点击引导程序的下拉菜单时总会收到错误

0 个答案:

没有答案