查找活动子菜单并删除其类

时间:2018-01-12 19:18:00

标签: javascript jquery

我有一个主菜单和子菜单。当我单击主菜单时,它会显示其内容。单击子菜单时,显示子菜单内容。但是当我点击子菜单后的主菜单时,它会显示主菜单内容以及子菜单内容,因为它不会删除子菜单活动类。我尝试了以下代码来查找活动子菜单的ID并将其删除。

var ref = $("ul#submenu li a").find(".active");
var ide= ref.attr("id");
$('#ide').removeClass("active");

为了更好地理解我的问题,我附上了一个jsfiddle https://jsfiddle.net/0pmzzp7e/11/

3 个答案:

答案 0 :(得分:1)

这是你想要的吗?

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">


<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container">
        <a class="navbar-brand" href="#">
           <img src="https://picsum.photos/140/" width="140" height="140" alt="Logo">
       </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 flex-column align-items-end" id="navbarCollapse">
            <!-- navbar1 -->
            <div class="navbar-nav mb-lg-5" style="font-size: 80%;">
                <span class="navbar-text">Logged in as:</span>
                <a class="nav-item nav-link" href="#">User</a>
                <!-- <a class="nav-item nav-link" href="#">Login</a> -->
            </div>

            <!-- navbar2 -->
            <ul class="navbar-nav mt-4">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">About</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Features</a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Products</a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Another action</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Something else here</a>
                    </div>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Free Cookies</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>


<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>

https://jsfiddle.net/h1afncux/1/

答案 1 :(得分:1)

请不要使用内联JavaScript,尽量将JavaScript和HTML分开。

&#13;
&#13;
$("ul a[href=\"#home\"]").on("click", function () {
  $(".active").removeClass("active");
  $("#home-content").addClass("active");
});
$("ul a[href=\"#contact\"]").on("click", function () {
  $(".active").removeClass("active");
  $("#contact-content").addClass("active");
});
&#13;
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-sm-12 container">
  <ul class="nav nav-tabs" id="main-menu">
    <li class="active">
      <a href="#home" data-toggle="tab">Home</a>
    </li>
    <li>
      <a href="#contact" data-toggle="tab">Contact</a>
    </li>
  </ul>
  <div class="tab-content clear-fix">
    <!-- Home-->
    <div class="tab-pane active" id="home">
      <ul class="nav nav-tabs">
        <li>
          <a href="#home1" data-toggle="tab">Home 1</a>
        </li>
        <li>
          <a href="#home2" data-toggle="tab">Home 2</a>
        </li>
      </ul>
      <div class="tab-content clear-fix">
        <div class="tab-pane active" id="home-content"> Home Content </div>
        <div class="tab-pane" id="home1"> Home 1 Submenu</div>
        <div class="tab-pane" id="home2">Home 2 Submenu</div>
      </div>
    </div>
    <!-- Contact-->
    <div class="tab-pane" id="contact">
      <ul class="nav nav-tabs">
        <li>
          <a href="#contact1" data-toggle="tab">Contact 1</a>
        </li>
        <li>
          <a href="#contact2" data-toggle="tab">Contact 2</a>
        </li>
      </ul>
      <div class="tab-content clear-fix">
        <div class="tab-pane active" id="contact-content"> Contact Content</div>
        <div class="tab-pane" id="contact1">Contact1 Content</div>
        <div class="tab-pane" id="contact2">Contact2 Content</div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您目前只在子菜单中找到活动的标签。我假设您要做的是检查整个active部分中的任何tab-pane元素。如果这样做,您可以迭代所有找到的活动项并从中删除活动类:

var ref = $(".tab-pane .active")
$(ref).each(function(){
  $(this).removeClass("active");
})

这是一个分叉的JSFiddle: https://jsfiddle.net/2zd309eo/1/