繁体   English   中英

单击后如何更改引导程序 3 下拉背景颜色?

[英]How can I change bootstrap 3 dropdown background color after click?

前 后

问题是当我单击下拉文本 go 黑色时,我需要将其更改为白色。 当我删除#tab 颜色时,我将更改为#9e9e9d。

#tab{ 颜色:黑色 }

#tab:hover {
  color: white;
}

      <div class="collapse navbar-collapse" id="myNavbar">
    <ul class="nav navbar-nav">
      <li><a href="index.html" id="tab">HOME</a></li>
      <li><a href="Beverages.html" id="tab">MENU</a></li>
      <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="tab">PROMOTION <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Gift Cards</a></li>
          <li><a href="#">Christmas Bundle</a></li>
        </ul>
      </li>
      <li><a href="About.html" id="tab">ABOUT</a></li>
      <li><a href="Faq.html" id="tab">FAQ</a></li>
    </ul>
  </div>

我在jquery上给出了一个解决方案,因为你提到了bootstrap 该解决方案使用方法toggleClass()

有必要吗?

 $('.dropdown').on('click', function() { $(this).toggleClass('a_color_active'); $('.dropdown-menu').toggleClass('ul_color_active'); });
 .dropdown { background-color: pink; }.dropdown-menu { display: none; }.dropdown a, .dropdown-menu a { color: unset; }.dropdown.a_color_active { background-color: black; color: white; }.dropdown-menu.ul_color_active { display: block; background-color: black; color: white; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="tab">PROMOTION <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">Gift Cards</a></li> <li><a href="#">Christmas Bundle</a></li> </ul> </li>

第二:

 $('.dropdown').on('click', function() { $(this).toggleClass('a_color_active'); $('.dropdown-menu').toggleClass('ul_color_active'); });
 .nav.navbar-nav a { color: black; }.nav.navbar-nav a:hover { color: white; }.dropdown-menu { display: none; }.nav.navbar-nav a, .dropdown-menu a { color: unset; }.dropdown.a_color_active { background-color: black; color: white; }.dropdown-menu.ul_color_active { display: block; background-color: black; color: white; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="collapse navbar-collapse" id="myNavbar"> <ul class="nav navbar-nav"> <li><a href="index.html" id="tab">HOME</a></li> <li><a href="Beverages.html" id="tab">MENU</a></li> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="tab">PROMOTION <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">Gift Cards</a></li> <li><a href="#">Christmas Bundle</a></li> </ul> </li> <li><a href="About.html" id="tab">ABOUT</a></li> <li><a href="Faq.html" id="tab">FAQ</a></li> </ul> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM