Bootstrap 4 - Remove active class on nav-link

2019-07-26 14:17发布

问题:

I'm creating a Bootstrap 4 navbar with the nav-item that have the active class. My problem is when I use the active class I can't remove it from the nav-item

How can I remove the active class on the nav-item?

This my HTML:

<nav class="navbar navbar-expand-lg navbar-light">
       <div class="container-fluid">
            <a class="navbar-brand" href="#">
               <small>LOGO BRAND</small>
            </a>
             <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-
             label="Toggle navigation">
                 <span class="navbar-toggler-icon"></span>
             </button>

        <div class="collapse navbar-collapse" id="navbarCollapse">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item">
              <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">About us</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Services</a>
            </li>
            <li class="nav-item dropdown menu-large">
              <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">   
               Dropdown Menu</a>
              <ul class="dropdown-menu dropdown-menu-custom megamenu megamenu-custom">
                <div class="row">
                  <li class="col-md-3">
                    <ul>
                      <li class="dropdown-header">Glyphicons</li>
                      <li><a href="#">Available glyphs</a></li>
                      <li class="disabled"><a href="#">How to use</a></li>
                      <li><a href="#">Examples</a></li>
                      <li class="divider"></li>
                      <li class="dropdown-header">Dropdowns</li>
                      <li><a href="#">Example</a></li>
                      <li><a href="#">Aligninment options</a></li>
                      <li><a href="#">Headers</a></li>
                      <li><a href="#">Disabled menu items</a></li>
                    </ul>
                  </li>
                  <li class="col-md-3">
                    <ul>
                      <li class="dropdown-header">Button groups</li>
                      <li><a href="https://www.google.it" target="_blank">Basic example</a>
                      </li>
                      <li><a href="#">Button toolbar</a>
                      </li>
                      <li><a href="#">Sizing</a>
                      </li>
                      <li><a href="#">Nesting</a>
                      </li>
                      <li><a href="#">Vertical variation</a>
                      </li>
                      <li class="divider"></li>
                      <li class="dropdown-header">Button dropdowns</li>
                      <li><a href="#">Single button dropdowns</a>
                      </li>
                    </ul>
                  </li>
                  <li class="col-md-3">
                    <ul>
                      <li class="dropdown-header">Input groups</li>
                      <li><a href="#">Basic example</a>
                      </li>
                      <li><a href="#">Sizing</a>
                      </li>
                      <li><a href="#">Checkboxes and radio addons</a>
                      </li>
                      <li class="divider"></li>
                      <li class="dropdown-header">Navs</li>
                      <li><a href="#">Tabs</a>
                      </li>
                      <li><a href="#">Pills</a>
                      </li>
                      <li><a href="#">Justified</a>
                      </li>
                    </ul>
                  </li>
                  <li class="col-md-3">
                    <ul>
                      <li class="dropdown-header">Navbar</li>
                      <li><a href="#">Default navbar</a>
                      </li>
                      <li><a href="#">Buttons</a>
                      </li>
                      <li><a href="#">Text</a>
                      </li>
                      <li><a href="#">Non-nav links</a>
                      </li>
                      <li><a href="#">Component alignment</a>
                      </li>
                      <li><a href="#">Fixed to top</a>
                      </li>
                      <li><a href="#">Fixed to bottom</a>
                      </li>
                      <li><a href="#">Static top</a>
                      </li>
                      <li><a href="#">Inverted navbar</a>
                      </li>
                    </ul>
                  </li>
                </div>
              </ul>
            </li>
          </ul>
        </div>
      </div>
    </nav>

This is my CSS:

.menu-large {position: static !important;}

.megamenu {
  padding: 20px 20px;
  width: 100%;
  -webkit-box-shadow: none;
  box-shadow: none;
}


.megamenu > div > li > ul {
  padding: 0;
  margin: 0;
}

.megamenu > div > li > ul > li {
  list-style: none;
}

.megamenu > div > li > ul > li > a {
  display: block;
  padding: 3px 20px;
  clear: both;
  font-weight: normal;
  line-height: 1.428571429;
  color: #333333;
  white-space: normal;
}

.megamenu > div > li > ul > li > a:hover,
.megamenu > div > li > ul > li > a:focus {
  text-decoration: none;
  color: #262626;
  background: none;
  /*background-color: #f5f5f5;*/
}

.megamenu .dropdown-header {
  color: #428bca;
  font-size: 18px;
  padding: 3px 20px;
  font-weight: bold;
}

.navbar-light .navbar-nav .active>.nav-link{
  color:red!important;
}

This is my JS:

$(document).ready(function() {
   $('.navbar-nav .nav-item').click(function(e) {
       $('.navbar-nav .nav-item').removeClass('active');
       var $this = $(this);
       if(!$this.hasClass('active')){
           $this.addClass('active');
       }
       e.preventDefault();
   });
});

JSFiddle Link where you can see the example JSFiddle

Can you help me to resolve the problem?.