如何将悬停规则放在Bootstrap DropDown按钮之上

时间:2018-06-02 15:59:28

标签: javascript jquery css twitter-bootstrap-3

您能否请看一下这段代码,并告诉我如何在.btn-default上保持悬停规则受影响,同时仍悬停在.dropdown-menu



$(function(){
    $('.btn-dropdown').hover(function() {
        $(this).addClass('open');
    },
    function() {
        $(this).removeClass('open');
    });
});

body{
padding:30px;
}
.btn-default:hover{
    color:red !important;
  }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="col-md-3">
<div class="btn-group btn-group-justified" role="group" aria-label="...">
  <div class="btn-group btn-dropdown" role="group">
    <a role="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      List One
      <i class="fa fa-chevron-right" aria-hidden="true"></i>
    </a>
    <ul class="dropdown-menu">
      <li><a href="#">Dropdown link</a></li>
      <li><a href="#">Dropdown link</a></li>
    </ul>
  </div>
  
<div class="btn-group btn-dropdown" role="group">
    <a role="button" class="btn  btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      List Two
      <i class="fa fa-chevron-right" aria-hidden="true"></i>
    </a>
    <ul class="dropdown-menu">
      <li><a href="#">Dropdown link</a></li>
      <li><a href="#">Dropdown link</a></li>
    </ul>
  </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

请参阅这是否是您正在寻找的内容。我在您的CSS中添加了.open个父亲,并删除了.hover

每当你附加.open课程时,它都会添加CSS

&#13;
&#13;
$(function(){
    $('.btn-dropdown').hover(function() {
        $(this).addClass('open');
    },
    function() {
        $(this).removeClass('open');
    });
});
&#13;
body{
padding:30px;
}
.open .btn-default{
    color:red !important;
  }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="col-md-3">
<div class="btn-group btn-group-justified" role="group" aria-label="...">
  <div class="btn-group btn-dropdown" role="group">
    <a role="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      List One
      <i class="fa fa-chevron-right" aria-hidden="true"></i>
    </a>
    <ul class="dropdown-menu">
      <li><a href="#">Dropdown link</a></li>
      <li><a href="#">Dropdown link</a></li>
    </ul>
  </div>
  
<div class="btn-group btn-dropdown" role="group">
    <a role="button" class="btn  btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      List Two
      <i class="fa fa-chevron-right" aria-hidden="true"></i>
    </a>
    <ul class="dropdown-menu">
      <li><a href="#">Dropdown link</a></li>
      <li><a href="#">Dropdown link</a></li>
    </ul>
  </div>
  </div>
</div>
&#13;
&#13;
&#13;