如何在访问/激活子元素时更改父元素的颜色

时间:2017-02-14 12:08:59

标签: html css html5 css3

我有一个下拉导航。我想要做的是当我点击任何子菜单链接时,我希望当链接处于活动状态时,父按钮的颜色保持不变。 这是代码的小提琴链接。 https://jsfiddle.net/v28ydshL/

<nav>
                <div class="navbar-header">
                    <button style="background-color: #3382d5;" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#menubar">
                        <span style="background-color: white;" class="icon-bar"></span>
                        <span style="background-color: white;" class="icon-bar"></span>
                        <span style="background-color: white;" class="icon-bar"></span>                        
                    </button>
                </div>
                <div id="menubar" class=" collapse navbar-collapse row">
                    <ul id="dropdownNavbar" class="nav navbar-nav">
                        <li class="dropdown">
                            <button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-time"></span> Time Card
                                <span class="caret"></span></button>
                            <div class="dropdown-content">
                                <a id="showTimeCard" href="#">My Time Card</a>
                                <a href="#">Sub Menu 2</a>
                                <a href="#">Sub Menu 3</a>
                                <a href="#">Sub Menu 4</a>
                                <a href="#">Sub Menu 5</a>
                            </div>
                        </li>
                        <li class="dropdown">
                            <button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-stats"></span> Project
                                <span class="caret"></span></button>
                            <div class="dropdown-content">
                                <a href="#">Sub Menu 1</a>
                                <a href="#">Sub Menu 2</a>
                                <a href="#">Sub Menu 3</a>
                            </div>
                        </li>
                        <li class="dropdown">
                            <button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-user"></span> HR Links
                                <span class="caret"></span></button>
                            <div class="dropdown-content">
                                <a href="#">Sub Menu 1</a>
                                <a href="#">Sub Menu 2</a>
                            </div>
                        </li>
                        <li class="dropdown">
                            <button type="button" class="btn dropbtn">
                                <span class="glyphicon glyphicon-screenshot"></span> Leave Tracker
                                <span class="caret"></span></button>
                            <div class="dropdown-content">
                                <a href="#">Sub Menu 1</a>
                                <a href="#">Sub Menu 2</a>
                                <a href="#">Sub Menu 3</a>
                                <a href="#">Sub Menu 4</a>
                            </div>
                        </li>
                        <li class="dropdown">
                            <button type="button" class="btn dropbtn">
                                <span class="glyphicon glyphicon-briefcase"></span> Accounts
                                <span class="caret"></span></button>
                            <div class="dropdown-content">
                                <a href="#">Sub Menu 1</a>
                                <a href="#">Sub Menu 2</a>
                                <a href="#">Sub Menu 3</a>
                                <a href="#">Sub Menu 4</a>
                                <a href="#">Sub Menu 5</a>
                            </div>
                        </li>
                        <li class="dropdown">
                            <button type="button" class="btn dropbtn">
                                <span class="glyphicon glyphicon-headphones"></span> Service Desk
                                <span class="caret"></span></button>
                            <div class="dropdown-content">
                                <a href="#">Sub Menu 1</a>
                                <a href="#">Sub Menu 2</a>
                                <a href="#">Sub Menu 3</a>
                                <a href="#">Sub Menu 4</a>
                            </div>
                        </li>
                        <li class="dropdown">
                            <button type="button" class="btn dropbtn">
                                <span class="glyphicon glyphicon-file"></span> Reports
                                <span class="caret"></span></button>
                            <div class="dropdown-content">
                                <a href="#">Sub Menu 1</a>
                                <a href="#">Sub Menu 2</a>
                                <a href="#">Sub Menu 3</a>
                                <a href="#">Sub Menu 4</a>
                                <a href="#">Sub Menu 5</a>
                            </div>
                        </li>
                        <li class="dropdown">
                            <button type="button" class="btn dropbtn">
                                <span class="glyphicon glyphicon-cog"></span> Settings
                                <span class="caret"></span></button>
                            <div class="dropdown-content">
                                <a href="#">Sub Menu 1</a>
                                <a href="#">Sub Menu 2</a>
                                <a href="#">Sub Menu 3</a>
                            </div>
                        </li> 
                    </ul>
                </div>
            </nav>

这是我的css

ul {
    list-style-type: none;
    padding-left: 1px;

}
.dropdown {
    float: left;
    padding-right:1px;
}
.dropbtn{
    background-color: #060975;
    height:30px;
    border-radius: 0px 10px;
}
li button, .dropbtn {
    display: inline-block;
    background-color:#3382d5;
    color: white;
    text-align: center;
}
li button:hover, .dropdown:hover .dropbtn {
    background-color: #31b0d5;
}
button.dropdown {
    display: inline-block;
}
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #e8f3f4;
    min-width: 100%;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.1);
    z-index: 10;
}
.navbar-header{
    overflow: visible;
    z-index: 1;
}
.dropdown-content a {
    color: black;
    padding: 5px 10px;
    display: block;
    text-align: left;
}
.dropdown-content a:hover {
    background-color: #d8dee2
}
.dropdown-content a:visited .dropbtn{
    background-color: #e8f3f4;
}
.dropdown:hover .dropdown-content {
    display: block;
}
#menubar{
    padding-top: 5px;
    overflow: visible;
    z-index: 10;
    padding-left:0px;
    padding-right: 0px;
    margin: 0px;
}
#dropdownNavbar{
    margin: 0px;
}

3 个答案:

答案 0 :(得分:0)

由于您使用的是Bootstrap - 您还必须使用jquery - 这是一个jquery方法。为所需的活动颜色创建一个类,然后单击 - 从所有按钮中删除该类,然后将其应用于单击的那个。

&#13;
&#13;
$(document).ready(function() {
  $('.dropdown').click(function() {
    $('.dropbtn').removeClass('dropbtn-active');
    $(this).find('.dropbtn').addClass('dropbtn-active')
  });

})
&#13;
ul {
  list-style-type: none;
  padding-left: 1px;
}
.dropdown {
  float: left;
  padding-right: 1px;
}
.dropbtn {
  background-color: #060975;
  height: 30px;
  border-radius: 0px 10px;
}
li button,
.dropbtn {
  display: inline-block;
  background-color: #3382d5;
  color: white;
  text-align: center;
}
.dropbtn-active {
  background-color: #31b0d5;
}
li button:hover,
.dropdown:hover .dropbtn {
  background-color: #31b0d5;
}
.dropdown:active .dropbtn {
  background-color: #31b0d5;
}
button.dropdown {
  display: inline-block;
}
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #e8f3f4;
  min-width: 100%;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.1);
  z-index: 10;
}
.navbar-header {
  overflow: visible;
  z-index: 1;
}
.dropdown-content a {
  color: black;
  padding: 5px 10px;
  display: block;
  text-align: left;
}
.dropdown-content a:hover {
  background-color: #d8dee2
}
.dropdown-content a:visited .dropbtn {
  background-color: #e8f3f4;
}
.dropdown:hover .dropdown-content {
  display: block;
}
#menubar {
  padding-top: 5px;
  overflow: visible;
  z-index: 10;
  padding-left: 0px;
  padding-right: 0px;
  margin: 0px;
}
#dropdownNavbar {
  margin: 0px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div class="navbar-header">
    <button style="background-color: #3382d5;" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#menubar">
      <span style="background-color: white;" class="icon-bar"></span>
      <span style="background-color: white;" class="icon-bar"></span>
      <span style="background-color: white;" class="icon-bar"></span> 
    </button>
  </div>
  <div id="menubar" class=" collapse navbar-collapse row">
    <ul id="dropdownNavbar" class="nav navbar-nav">
      <li class="dropdown">
        <button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-time"></span> Time Card
          <span class="caret"></span>
        </button>
        <div class="dropdown-content">
          <a id="showTimeCard" href="#">My Time Card</a>
          <a href="#">Sub Menu 2</a>
          <a href="#">Sub Menu 3</a>
          <a href="#">Sub Menu 4</a>
          <a href="#">Sub Menu 5</a>
        </div>
      </li>
      <li class="dropdown">
        <button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-stats"></span> Project
          <span class="caret"></span>
        </button>
        <div class="dropdown-content">
          <a href="#">Sub Menu 1</a>
          <a href="#">Sub Menu 2</a>
          <a href="#">Sub Menu 3</a>
        </div>
      </li>
      <li class="dropdown">
        <button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-user"></span> HR Links
          <span class="caret"></span>
        </button>
        <div class="dropdown-content">
          <a href="#">Sub Menu 1</a>
          <a href="#">Sub Menu 2</a>
        </div>
      </li>
      <li class="dropdown">
        <button type="button" class="btn dropbtn">
          <span class="glyphicon glyphicon-screenshot"></span> Leave Tracker
          <span class="caret"></span>
        </button>
        <div class="dropdown-content">
          <a href="#">Sub Menu 1</a>
          <a href="#">Sub Menu 2</a>
          <a href="#">Sub Menu 3</a>
          <a href="#">Sub Menu 4</a>
        </div>
      </li>
      <li class="dropdown">
        <button type="button" class="btn dropbtn">
          <span class="glyphicon glyphicon-briefcase"></span> Accounts
          <span class="caret"></span>
        </button>
        <div class="dropdown-content">
          <a href="#">Sub Menu 1</a>
          <a href="#">Sub Menu 2</a>
          <a href="#">Sub Menu 3</a>
          <a href="#">Sub Menu 4</a>
          <a href="#">Sub Menu 5</a>
        </div>
      </li>
      <li class="dropdown">
        <button type="button" class="btn dropbtn">
          <span class="glyphicon glyphicon-headphones"></span> Service Desk
          <span class="caret"></span>
        </button>
        <div class="dropdown-content">
          <a href="#">Sub Menu 1</a>
          <a href="#">Sub Menu 2</a>
          <a href="#">Sub Menu 3</a>
          <a href="#">Sub Menu 4</a>
        </div>
      </li>
      <li class="dropdown">
        <button type="button" class="btn dropbtn">
          <span class="glyphicon glyphicon-file"></span> Reports
          <span class="caret"></span>
        </button>
        <div class="dropdown-content">
          <a href="#">Sub Menu 1</a>
          <a href="#">Sub Menu 2</a>
          <a href="#">Sub Menu 3</a>
          <a href="#">Sub Menu 4</a>
          <a href="#">Sub Menu 5</a>
        </div>
      </li>
      <li class="dropdown">
        <button type="button" class="btn dropbtn">
          <span class="glyphicon glyphicon-cog"></span> Settings
          <span class="caret"></span>
        </button>
        <div class="dropdown-content">
          <a href="#">Sub Menu 1</a>
          <a href="#">Sub Menu 2</a>
          <a href="#">Sub Menu 3</a>
        </div>
      </li>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

看看

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script src="http://code.jquery.com/jquery-1.11.1.js" type="application/javascript"></script>
    </head>
    <style>
    #menu, #menu ul {
        margin: 0;
        padding: 0;
        list-style: none;
    } #menu {
        width: 958px;
        /*margin: 60px auto;*/
        border: 1px solid #222;
        background-color: #111;
        background-image: linear-gradient(#444, #111);

    box-shadow:
        0 1px 1px #777;
    } #menu:before, #menu:after {
        content: "";
        display: table;
    } #menu:after {
        clear: both;
    } #menu {
        zoom: 1;
    } #menu li {
        float: left;
        border-right: 1px solid #222;
        box-shadow: 1px 0 0 #444;
        position: relative;
    } #menu a {
        float: left;
        padding: 11px 40px;
        color: #999;
        text-transform: uppercase;
        text-decoration: none;
        text-shadow: 0 1px 0 #000;
    } #menu li:hover > a {
        color: #fafafa;
    } * html #menu li a:hover { /* IE6 only */
        color: #fafafa;
    } #menu ul {
        margin: 20px 0 0 0;
        _margin: 0; /*IE6 only*/
        opacity: 0;
        visibility: hidden;
        position: absolute;
        top: 38px;
        left: 0;
        right: -1px;
        z-index: 1000000;
        background: #444;
        background: linear-gradient(#444, #111);

    box-shadow:
        0 -1px 0 rgba(255, 255, 255, .3);

    transition:all
        .2s

    ease-in-out;
    } #menu li:hover > ul {
        opacity: 1;
        visibility: visible;
        margin: 0;
    } #menu ul ul {
        top: 0;
        left: 150px;
        margin: 0 0 0 20px;
        _margin: 0; /*IE6 only*/
        box-shadow: -1px 0 0 rgba(255, 255, 255, .3);
    } #menu ul li {
        float: none;
        display: block;
        border: 0;
        _line-height: 0; /*IE6 only*/
        box-shadow: 0 1px 0 #111, 0 2px 0 #666;
    } #menu ul li:last-child {
        box-shadow: none;
    } #menu ul a {
        padding: 10px;
        _height: 10px; /*IE6 only*/
        display: block;
        white-space: nowrap;
        float: none;
        text-transform: none;
    } #menu ul a:hover {
        background-color: #000000;
    } #menu ul li:first-child > a {
        /*border-radius: 3px 3px 0 0;*/
        padding: 11px 13px;
    } #menu ul li:first-child > a:after {
        content: '';
        position: absolute;
        left: 40px;
        top: -6px;
    } #menu ul ul li:first-child a:after {
        left: -6px;
        top: 50%;
        margin-top: -6px;
        border-left: 0;
    } #menu ul li:first-child a:hover:after {
        border-bottom-color: #000;
    } #menu ul ul li:first-child a:hover:after {
        border-right-color: #000;
        border-bottom-color: transparent;
    } #menu ul li:last-child > a {
        /*border-radius: 0 0 3px 3px*/;
        padding: 11px 20px;
    }

    #menu li.active{
        background: #ccddff;
    }
    </style>
    <body>
    <ul id="menu">
        <li><a href="#">About </a></li>
        <li><a href="#">Programmes</a>
            <ul class="secondLevel">
                <li><a href="">Undergraduate</a></li>
                <li><a href="#">Postgraduate</a></li>
            </ul>
        </li>
        <li><a href="#">Academics</a>
         <ul class="secondLevel">
                <li><a  href="">Undergraduate</a></li>
                <li><a href="#">Postgraduate</a></li>
            </ul>
        </li>
        <li><a href="#">Research</a></li>
        <li><a href="#">Facilities</a></li>
    </ul>
    </body>
    <script>
     $('.secondLevel').find('li').click(function(){

           //removing the previous selected menu state
           $('#menu').find('li').removeClass('active');

           //adding the state for this parent menu
           $(this).parents('li').addClass('active');
        });
    </script>
    </html>

答案 2 :(得分:-1)

这是一个有效的例子。

设置一个活动的类并添加一些css。

&#13;
&#13;
$('.dropdown-content a').on("click", function() {
    $('.dropbtn').removeClass("active");
    $(this).parent().parent().find(".dropbtn").addClass("active");
});
&#13;
ul {
    list-style-type: none;
    padding-left: 1px;
}

.dropbtn.active {
  background: red;
}

.dropdown {
    float: left;
    padding-right:1px;
}

.dropbtn{
    background-color: #060975;
    height:30px;
    border-radius: 0px 10px;
}
li button, .dropbtn {
    display: inline-block;
    background-color:#3382d5;
    color: white;
    text-align: center;
}

li button:hover, .dropdown:hover .dropbtn {
    background-color: #31b0d5;
}

button.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #e8f3f4;
    min-width: 100%;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.1);
    z-index: 10;
}

.navbar-header{
    overflow: visible;
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 5px 10px;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {
    background-color: #d8dee2
}
.dropdown-content a:visited .dropbtn{
    background-color: #e8f3f4;
}

.dropdown:hover .dropdown-content {
    display: block;
}

#menubar{
    padding-top: 5px;
    overflow: visible;
    z-index: 10;
    padding-left:0px;
    padding-right: 0px;
    margin: 0px;
    
}

#dropdownNavbar{
    margin: 0px;
}
&#13;
<nav>
    <div class="navbar-header">
        <button style="background-color: #3382d5;" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#menubar">
            <span style="background-color: white;" class="icon-bar"></span>
            <span style="background-color: white;" class="icon-bar"></span>
            <span style="background-color: white;" class="icon-bar"></span>
        </button>
    </div>
    <div id="menubar" class=" collapse navbar-collapse row">
        <ul id="dropdownNavbar" class="nav navbar-nav">
            <li class="dropdown">
                <button type="button" class="btn dropbtn">
                    <span class="glyphicon glyphicon-time"></span> Time Card
                    <span class="caret"></span>
                </button>
                <div class="dropdown-content">
                    <a id="showTimeCard" href="#">My Time Card</a>
                    <a href="#">Sub Menu 2</a>
                    <a href="#">Sub Menu 3</a>
                    <a href="#">Sub Menu 4</a>
                    <a href="#">Sub Menu 5</a>
                </div>
            </li>
            <li class="dropdown">
                <button type="button" class="btn dropbtn">
                    <span class="glyphicon glyphicon-stats"></span> Project
                    <span class="caret"></span>
                </button>
                <div class="dropdown-content">
                    <a href="#">Sub Menu 1</a>
                    <a href="#">Sub Menu 2</a>
                    <a href="#">Sub Menu 3</a>
                </div>
            </li>
            <li class="dropdown">
                <button type="button" class="btn dropbtn">
                    <span class="glyphicon glyphicon-user"></span> HR Links
                    <span class="caret"></span>
                </button>
                <div class="dropdown-content">
                    <a href="#">Sub Menu 1</a>
                    <a href="#">Sub Menu 2</a>
                </div>
            </li>
            <li class="dropdown">
                <button type="button" class="btn dropbtn">
                    <span class="glyphicon glyphicon-screenshot"></span> Leave Tracker
                    <span class="caret"></span>
                </button>
                <div class="dropdown-content">
                    <a href="#">Sub Menu 1</a>
                    <a href="#">Sub Menu 2</a>
                    <a href="#">Sub Menu 3</a>
                    <a href="#">Sub Menu 4</a>
                </div>
            </li>
            <li class="dropdown">
                <button type="button" class="btn dropbtn">
                    <span class="glyphicon glyphicon-briefcase"></span> Accounts
                    <span class="caret"></span>
                </button>
                <div class="dropdown-content">
                    <a href="#">Sub Menu 1</a>
                    <a href="#">Sub Menu 2</a>
                    <a href="#">Sub Menu 3</a>
                    <a href="#">Sub Menu 4</a>
                    <a href="#">Sub Menu 5</a>
                </div>
            </li>
            <li class="dropdown">
                <button type="button" class="btn dropbtn">
                    <span class="glyphicon glyphicon-headphones"></span> Service Desk
                    <span class="caret"></span>
                </button>
                <div class="dropdown-content">
                    <a href="#">Sub Menu 1</a>
                    <a href="#">Sub Menu 2</a>
                    <a href="#">Sub Menu 3</a>
                    <a href="#">Sub Menu 4</a>
                </div>
            </li>
            <li class="dropdown">
                <button type="button" class="btn dropbtn">
                    <span class="glyphicon glyphicon-file"></span> Reports
                    <span class="caret"></span>
                </button>
                <div class="dropdown-content">
                    <a href="#">Sub Menu 1</a>
                    <a href="#">Sub Menu 2</a>
                    <a href="#">Sub Menu 3</a>
                    <a href="#">Sub Menu 4</a>
                    <a href="#">Sub Menu 5</a>
                </div>
            </li>
            <li class="dropdown">
                <button type="button" class="btn dropbtn">
                    <span class="glyphicon glyphicon-cog"></span> Settings
                    <span class="caret"></span>
                </button>
                <div class="dropdown-content">
                    <a href="#">Sub Menu 1</a>
                    <a href="#">Sub Menu 2</a>
                    <a href="#">Sub Menu 3</a>
                </div>
            </li>
        </ul>
    </div>
</nav>
&#13;
&#13;
&#13;