我正在创建一个切换按钮,当单击一行文本时,下面将显示更多文本,再次单击时,文本将折叠。我正在尝试使文本颜色在折叠时为蓝色,在打开时为红色。我有这个工作,除了在初始页面加载时,文本颜色不会显示为折叠颜色。谢谢您的帮助。
.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>
答案 0 :(得分:1)
之所以发生这种情况,是因为仅在折叠手风琴真正折叠至少一次之后才添加类for (i in 1:length(test)){
print(test[i])
}
,因此在加载页面时不会添加折叠
要解决此问题,您可以为文本.collapsed
添加第三个状态,该状态也具有蓝色。然后,您需要在打开手风琴后立即使用javascript手动删除此状态,从而使其恢复初始状态。
这是一个示范:
.initial
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;
}