如何在折叠/折叠时更改Bootstrap手风琴标题

时间:2017-04-04 18:56:58

标签: jquery css twitter-bootstrap accordion panels

折叠所有面板时,背景颜色应为白色。面板打开时,面板标题背景颜色应为灰色。我该怎么办?

      <div class="panel panel-simple">
        <div class="panel-heading" role="tab" id="headingOneMaintenance">
          <h4 class="panel-title">
            <a class="accordion-toggle" role="button" data-toggle="collapse" data-parent="#accordionMaintenance" href="#collapseOneMaintenance" aria-expanded="true" aria-controls="collapseOneMaintenance">
              What will you do to prepare my apartment before I move in? 
            </a>
          </h4>
        </div>
        <div id="collapseOneMaintenance" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOneMaintenance">
          <div class="panel-body">
            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
          </div>
        </div>
      </div>

使用这个CSS,我的所有面板都是白色的,但是当面板打开时,我希望标题是灰色的:

.panel-heading {
   background-color: white;}

尽管如此,它并不一定是这个CSS。谢谢!

2 个答案:

答案 0 :(得分:2)

你可以通过CSS边距&amp;和.accordion-toggle做一点点破解填充以覆盖所有面板尺寸:

以下代码段:

.accordion-toggle {
  background-color: gray;
  display: block;
  padding: 10px;
  margin: -10px -15px;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}

.accordion-toggle.collapsed {
  background-color: white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="panel panel-simple">
  <div class="panel-heading" role="tab" id="headingOneMaintenance">
    <h4 class="panel-title">
      <a class="accordion-toggle" role="button" data-toggle="collapse" data-parent="#accordionMaintenance" href="#collapseOneMaintenance" aria-expanded="true" aria-controls="collapseOneMaintenance">
        What will you do to prepare my apartment before I move in?
      </a>
    </h4>
  </div>
  <div id="collapseOneMaintenance" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOneMaintenance">
    <div class="panel-body">
      Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
      on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw
      denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
    </div>
  </div>
</div>

答案 1 :(得分:1)

试试这段代码:

 $(document).on('click', '.panel-heading', function(){
    $('.panel-heading').css('background-color', 'grey');
    if ($(this).find('.accordion-toggle').hasClass('collapsed')) {
        $(this).css('background-color', 'white');
    }

 })
.panel-heading {
   background-color: grey;}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="panel panel-simple">
  <div class="panel-heading" role="tab" id="headingOneMaintenance">
    <h4 class="panel-title">
      <a class="accordion-toggle" role="button" data-toggle="collapse" data-parent="#accordionMaintenance" href="#collapseOneMaintenance" aria-expanded="true" aria-controls="collapseOneMaintenance">
        What will you do to prepare my apartment before I move in? 
      </a>
    </h4>
  </div>
  <div id="collapseOneMaintenance" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOneMaintenance">
    <div class="panel-body">
      Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
    </div>
  </div>
</div>