Javascript 在Bootstrap中,如何识别;主动的;导航元素?

Javascript 在Bootstrap中,如何识别;主动的;导航元素?,javascript,twitter-bootstrap,scrollspy,Javascript,Twitter Bootstrap,Scrollspy,我正在使用Twitter Bootstrap 3.0,在向下滚动时使用该插件激活我的“导航”链接。现在我想自动更新地址栏中的URL(使用window.history.pushstate)以响应ScrollSpy事件“activate.bs.ScrollSpy” 如何识别ScrollSpy为我激活的“导航”元素? 我的代码看起来像这样 <body data-spy="scroll" data-target="#primarynavbar"> <div id="primar

我正在使用Twitter Bootstrap 3.0,在向下滚动时使用该插件激活我的“导航”链接。现在我想自动更新地址栏中的URL(使用window.history.pushstate)以响应ScrollSpy事件“activate.bs.ScrollSpy”

如何识别ScrollSpy为我激活的“导航”元素?

我的代码看起来像这样

<body data-spy="scroll" data-target="#primarynavbar">
    <div id="primarynavbar" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
...
...
        <li><a href="#contact">Contact Us</a></li>
...
...
    <div id="contact" />
...
...
    <script>
        $('#primarynavbar').on('activate.bs.scrollspy', function () {
            window.history.pushstate('http://abcd.com#contact')
        });
    </script>
...

...
...
  • ... ... ... ... $('primarynavbar')。on('activate.bs.scrollspy',function(){ window.history.pushstate('http://abcd.com#contact') }); ...
    我假设您可以查找活动类,并获取子锚href属性:

    $('#primarynavbar').on('activate.bs.scrollspy', function () {
       var hash = $(this).find("li.active a").attr("href");
       window.history.pushstate('http://abcd.com' + hash);
    });
    

    我假设您可以查找活动类,并获取child-anchors href属性:

    $('#primarynavbar').on('activate.bs.scrollspy', function () {
       var hash = $(this).find("li.active a").attr("href");
       window.history.pushstate('http://abcd.com' + hash);
    });
    

    工作起来很有魅力。如果有嵌套菜单,则希望使用当前子菜单项。下面是您的选择器的pimped版本:
    var hash=$(this.find(“li.active:last a”).attr(“href”)就像一个符咒。如果有嵌套菜单,则希望使用当前子菜单项。下面是您的选择器的pimped版本:
    var hash=$(this.find(“li.active:last a”).attr(“href”)