我尝试了以下代码,除了FontAwather
图标外,一切都很好。我试图在Google上找到一些解决方案,但我找不到我正在寻找的解决方案。
当我点击其中一个手风琴时,另一个手风琴应该折叠,当前点击的手风琴应该打开,同时fa图标应该根据手风琴的位置而改变。
这是我到目前为止已经尝试过的
$(document).ready(function () {
$(".accordion-body").on("shown", function (evt) {
setIcon(evt.target, true);
});
$(".accordion-body").on("hidden", function (evt) {
setIcon(evt.target, false);
});
$(".accordion-body").collapse("hide");
$("#collapse-faq-1").collapse("show");
});
$('.accordion-toggle').click(function(event) {
var id = '#'+ event.currentTarget.firstElementChild.id;
if('class == fa-chevron-down'){
$(id).removeClass('fa-chevron-down');
$(id).addClass('fa-close');
}else{
$(id).removeClass('fa-close');
$(id).addClass('fa-chevron-down');
}
});
$(function () {
var active = true;
$('#collapse-init').click(function () {
if (active) {
active = false;
$('.panel-collapse').collapse('show');
$('.panel-title').attr('data-toggle', '');
$(this).text('Enable accordion behavior');
} else {
active = true;
$('.panel-collapse').collapse('hide');
$('.panel-title').attr('data-toggle', 'collapse');
$(this).text('Disable accordion behavior');
}
});
$('#accordion').on('show.bs.collapse', function () {
if (active) $('#accordion .in').collapse('hide');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="accordion" id="accordion">
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-1">
<i id="indicator-1" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-1" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-2">
<i id="indicator-2" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-2" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-3">
<i id="indicator-3" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-3" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-4">
<i id="indicator-4" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-4" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-5">
<i id="indicator-5" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-5" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-6">
<i id="indicator-6" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-6" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading WhiteBg" id="heads">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-7">
<i id="indicator-7" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-7" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading WhiteBg">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-8">
<i id="indicator-8" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-8" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
</div>
首先,您没有在代码中链接bootstrap.min.js
文件。如果您想使用bootstrap,那么您需要包含它的bootstrap.min.css
文件及其bootstrap.min.js
文件来为您执行所有操作。
您需要以下代码才能根据您的要求工作。通过使用not(this)
,它将为您提供除当前单击的项目之外的所有元素。这样您就可以在所有其他元素上重置Fa图标并在您的this元素上应用特定的图标。
$('.accordion-toggle').click(function (event) {
var $this = $(this);
// It will reset all the other icons except the clicked item icon
$('.accordion-toggle').not(this).children('i').removeClass('fa-close').addClass('fa-chevron-down');
if ($this.children('i').hasClass('fa-chevron-down')) {
$this.children('i').removeClass('fa-chevron-down').addClass('fa-close');
} else {
$this.children('i').removeClass('fa-close').addClass('fa-chevron-down');
}
});
这是正在工作的JSFiddle。
下面是您的问题的嵌入片段。
$(function () {
$(document).ready(function () {
$(".accordion-body").on("shown", function (evt) {
setIcon(evt.target, true);
});
$(".accordion-body").on("hidden", function (evt) {
setIcon(evt.target, false);
});
$(".accordion-body").collapse("hide");
$("#collapse-faq-1").collapse("show");
});
$('.accordion-toggle').click(function (event) {
var $this = $(this);
// It will reset all the icons except the clicked (this) item.
$('.accordion-toggle').not(this).children('i').removeClass('fa-close').addClass('fa-chevron-down');
if ($this.children('i').hasClass('fa-chevron-down')) {
$this.children('i').removeClass('fa-chevron-down').addClass('fa-close');
} else {
$this.children('i').removeClass('fa-close').addClass('fa-chevron-down');
}
});
$(function () {
var active = true;
$('#collapse-init').click(function () {
if (active) {
active = false;
$('.panel-collapse').collapse('show');
$('.panel-title').attr('data-toggle', '');
$(this).text('Enable accordion behavior');
} else {
active = true;
$('.panel-collapse').collapse('hide');
$('.panel-title').attr('data-toggle', 'collapse');
$(this).text('Disable accordion behavior');
}
});
$('#accordion').on('show.bs.collapse', function () {
if (active) $('#accordion .in').collapse('hide');
});
});
});
html prettyprint-override"><link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<div class="accordion" id="accordion">
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-1">
<i id="indicator-1" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-1" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-2">
<i id="indicator-2" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-2" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-3">
<i id="indicator-3" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-3" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-4">
<i id="indicator-4" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-4" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-5">
<i id="indicator-5" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-5" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-6">
<i id="indicator-6" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-6" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading WhiteBg" id="heads">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-7">
<i id="indicator-7" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-7" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
<div class="accordion-group items">
<div class="accordion-heading WhiteBg">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-faq-8">
<i id="indicator-8" class="fa fa-chevron-down pull-right"></i>
Question
</a>
</div>
<div id="collapse-faq-8" class="accordion-body collapse in">
<div class="accordion-inner">
<p>
Inner content
</p>
</div>
</div>
</div>
</div>
在引导手风琴中,我不需要单击文本,而是希望在单击div中的任何位置时使其折叠。 我正在使用引导程序3。所以它不是手风琴,而是一个可折叠的面板。你知道如何让整个面板都可以点击吗?
我正在使用href链接和可折叠标题制作手风琴菜单。问题是,当我单击子手风琴时,它会折叠父级。有什么解决方法吗? 这是更新的版本,它正在工作。解决方案是使用“”(如@Renan Arajo所述)并删除手风琴上的标题设置。 https://codepen.io/CocoSkin/pen/vRQyZP
所以我坚持使用一个插件,它提供了一个简单的手风琴,但不提供自动折叠。我试图创建一个简单的脚本,但我没有成功。 让我提供一些DOM结构: 假设结构不言自明,但有一点是:当类被删除时,它会扩展项目(在我看来,这与构建手风琴的方式相反...)。 不知何故,触发jQuery脚本的唯一方法是针对标头()。它应该做的是找到每个没有类并添加它,同时将其从父类中删除。 我最好的猜测是这段jQuery。 谁能告诉我
手风琴效果其实就是通过JS改变元素的height,然后加上transition来让css动起来。 准备HTML结构 这里我们使用 dl , 因为 dt 刚好可以模拟标题, dd 模拟内容。 <div class="panel"> <dl> <dt>One Item</dt> <dd>One Item Content .</dd>
我使用选择调用手风琴项目,如http://jsfiddle.net/Rnfzz/1/ 但是,当我从选择框中选择一个项目时,其他打开的手风琴项目不会自动隐藏。如何隐藏其他手风琴项目,以便仅显示单击的手风琴项目?。 这是html C-1 Anim pariatur cliche reprehenderit,enim eiusmod high life accusamus terry richardso
我有一个自定义手风琴,像jQuery UI手风琴。我还试图使任何展开的项目可折叠(如果再次单击)。类似这样:https://jqueryui.com/accordion/#collapsible但是我不知道如何在当前扩展的项目上捕获另一个单击事件。有人能给我指一下正确的方向吗? 我的jQuery如下所示: 这是jsbin:http://jsbin.com/hazaxunuwa/edit?html,