如何在下拉列表打开时激活链接?

时间:2017-06-29 06:31:23

标签: jquery html css

在bootstrap导航栏中,如何激活导航链接 (只需更改导航链接的bg颜色) 当导航链接的相应下拉列表打开时 或下拉菜单链接悬停? 我尝试了以下代码



$('ul.nav li.dropdown').hover(function() {
  $(this).find('.dropdown-menu').stop(true, true).delay(50).fadeIn(50);
	}, function() {
  $(this).find('.dropdown-menu').stop(true, true).delay(50).fadeOut(50);
});

.navbar-nav li a{
  color: #000;
  text-transform: uppercase;
  padding: 13px 10px;
  text-align: center;
}
.navbar-nav li a:hover{
  background-color: #238500;
  color: #fff;
}
.dropdown-menu li a{
  font-weight: normal !important;
  padding: 15px !important;
  color: #fff !important;
  text-transform: capitalize !important;
  text-align: left !important;
}
ul.dropdown-menu li a:hover{
  background-color: #3c3c3c;
  color: #fff;
  background-image: none;
}
.dropdown-menu > li > a:focus, .dropdown-menu > li > a:hover{
	background-image: none;
}
ul.dropdown-menu li a:hover > .navbar-nav li a{
	background-color: #238500 !important;
}
.dropdown-menu{
  background-color: #238500;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="nav navbar-nav">
  <li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Education</a>
    <ul class="dropdown-menu">
      <li><a href="#">Goals and Objectives</a></li>
      <li><a href="#">Governing priciples</a></li>
      <li><a href="#">Board of directors</a></li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

试试这个:

&#13;
&#13;
$('ul.nav li.dropdown').hover(function() {
  $(this).find('.dropdown-menu').stop(true, true).delay(50).fadeIn(50);
  $(this).addClass("hover");
	}, function() {
  $(this).find('.dropdown-menu').stop(true, true).delay(50).fadeOut(50);
  $(this).removeClass("hover");
});
&#13;
.navbar-nav li a{
  color: #000;
  text-transform: uppercase;
  padding: 13px 10px;
  text-align: center;
}
.navbar-nav li a:hover{
  background-color: #238500;
  color: #fff;
}
.dropdown-menu li a{
  font-weight: normal !important;
  padding: 15px !important;
  color: #fff !important;
  text-transform: capitalize !important;
  text-align: left !important;
}
ul.dropdown-menu li a:hover{
  background-color: #3c3c3c;
  color: #fff;
  background-image: none;
}
.dropdown-menu > li > a:focus, .dropdown-menu > li > a:hover{
	background-image: none;
}
ul.dropdown-menu li a:hover > .navbar-nav li a{
	background-color: #238500 !important;
}
.dropdown-menu{
  background-color: #238500;
}

.hover {
background-color: grey;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="nav navbar-nav">
  <li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Education</a>
    <ul class="dropdown-menu">
      <li><a href="#">Goals and Objectives</a></li>
      <li><a href="#">Governing priciples</a></li>
      <li><a href="#">Board of directors</a></li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

根据我的理解,这是你想要的吗?

.navbar-nav .open>a,
.navbar-nav .open>a:focus,
.navbar-nav .open>a:hover {
    background-color: #238500 ;
    color: #fff;
}

只需在CSS中添加此样式。