gpt4 book ai didi

javascript - 单击一个 Accordion 时,多个 Bootstrap Accordion 会折叠其他 Accordion

转载 作者:行者123 更新时间:2023-12-03 10:41:23 26 4
gpt4 key购买 nike

我尝试了以下代码,除了 FontAwesome 图标之外,一切都很好。我尝试在 Google 上寻找解决方案,但找不到我正在寻找的解决方案。

当我单击其中一个 Accordion 时,另一个 Accordion 应折叠,当前单击的 Accordion 应打开,同时 fa 图标应根据 Accordion 位置进行更改。

这是我迄今为止尝试过的JSFiddle

$(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 图标,并在您的此元素上应用特定图标。

$('.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');

}
});

Here is working 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');
});

});

});
<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>

关于javascript - 单击一个 Accordion 时,多个 Bootstrap Accordion 会折叠其他 Accordion ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28765037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com