使用.click()jquery添加类后,href不会添加url

时间:2017-12-07 17:47:07

标签: javascript jquery

CSS正常工作。当我点击它时,按钮变为活动状态。但是在URL的末尾,href的数据没有添加。如果我删除e.preventDefault();

,它也会很糟糕

提前谢谢。



(function() {
  'use strict';

  $("#header-navigation ul li").click(function(e) {
    e.preventDefault();
    $("#header-navigation ul li").removeClass("current-menu-item menu-item-has-children");
    $(this).addClass("current-menu-item menu-item-has-children");
  });

})();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="header-navigation-wrap col-md-8">
  <div id="extended-bg">
    <nav id="header-navigation" class="header-navigation clearfix">
      <ul class="header-menu clearfix">
        <li class="current-menu-item menu-item-has-children">
          <a href="/">Home</a>
        </li>
        <li><a href="/category">Category</a></li>
        <li><a href="/about">About</a></li>
        <li><a href="/feedback">Feedback</a></li>
      </ul>
    </nav>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

...最后

JS

(function () {
'use strict';

$('ul.header-menu li').click(function(){
    window.sessionStorage.activeMenuItem = this.id;
});

if (window.sessionStorage.activeMenuItem) {
    $("#"+sessionStorage.activeMenuItem).addClass('current-menu-item menu-item-has-children');
}
})();

HTML

<ul class="header-menu clearfix">
     <li id="liHome"><a href="/">Home</a></li>
     <li id="liCategory"><a href="/category">Category</a></li>
     <li id="liAbout"><a href="/about">About</a></li>
     <li id="liFeedback"><a href="/feedback">Feedback</a></li>
</ul>