【问题标题】:How can I set the initial state of the Bootstrap toggle to be collapsed?如何将 Bootstrap 切换的初始状态设置为折叠?
【发布时间】:2020-09-30 06:35:59
【问题描述】:

我正在创建一个切换,当单击一行文本时,更多文本会下降到下方,再次单击时,文本会折叠。我试图让文本颜色在折叠时为蓝色,在打开时为红色。我有这个工作,除了在初始页面加载时,文本颜色不会显示为折叠颜色。感谢您的帮助。

.accordion-toggle {
    color: rgb(176, 26, 33) !important;
}

.accordion-toggle.collapsed {
    color: #3a70a1 !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>

      <div class="panel-group" id="accordion">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h4 class="panel-title">
              <a class="accordion-toggle" data-toggle="collapse" href="#collapseOne" id="question1">

                Line of text.
              </a>
            </h4>
          </div>
          <div id="collapseOne" class="panel-collapse collapse in">
            <div class="panel-body answer">
                More text
            </div>
          </div>
        </div>
       </div>


<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>

【问题讨论】:

  • 看起来您正在为 Bootstrap 4 使用 3.x 标记。不再有 panelin

标签: css twitter-bootstrap bootstrap-4 toggle


【解决方案1】:

发生这种情况是因为 .collapsed 类仅在手风琴确实至少折叠一次后才被添加,因此不会在加载页面时添加。

要解决此问题,您可以为文本添加第三个状态.initial,它也具有蓝色。然后,您需要在手风琴打开后立即使用 javascript 手动删除此状态,从而离开其初始状态。

这是一个演示:

function removeInitial(el) {
  el.classList.remove("initial");
}
.accordion-toggle {
  color: rgb(176, 26, 33) !important;
}

.accordion-toggle.collapsed {
  color: #3a70a1 !important;
}

.initial {
  color: #3a70a1 !important;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>

<body>

  <div class="panel-group" id="accordion">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a class="accordion-toggle initial" data-toggle="collapse" href="#collapseOne" id="question1" onclick="removeInitial(this)">

                Line of text.
              </a>
        </h4>
      </div>
      <div id="collapseOne" class="panel-collapse collapse in">
        <div class="panel-body answer">
          More text
        </div>
      </div>
    </div>
  </div>


  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>

</html>

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2020-11-02
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多