如何在 collapse/collapsed 上更改 Bootstrap 手风琴 header

How to change the Bootstrap accordion header on collapse/collapsed

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

      <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,我所有的面板都是白色的,但是当面板打开时,我希望 header 是灰色的:

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

但不一定是这样 CSS。谢谢!

您可以对 .accordion-toggle 做一些改动,使用 CSS 边距和填充来覆盖所有面板尺寸:

下面的片段:

.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>

试试这个代码:

 $(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>