gpt4 book ai didi

javascript - 当模态被解除或关闭时如何关闭所有可折叠的 div( Accordion )?

转载 作者:行者123 更新时间:2023-11-28 04:33:03 25 4
gpt4 key购买 nike

我开发了一个包含三个可折叠 div 的模式。当我关闭模式并再次打开它时,事件的可折叠 div 仍然打开。当模态关闭时,我宁愿关闭所有打开的可折叠对象,除了带有“panel-collapse collapse in”类的 div。

PS- 我正在使用 Bootstrap 3。

<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 id="modal-title"> </h4>
</div>
<div class="modal-body">
<div class="panel-group" id="accordion">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse1">Course Introduction

<span class="pull-right"><i id="icon0" class="fa fa-plus open" aria-hidden="true"></i></span>
</a>

</h4>
</div>

<div id="collapse1" class="panel-collapse collapse in">
<div class="panel-body">
<p> abc abc abc </p>
</div>
</div>
</div>


<div class="panel ">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse2">Description
<span class="pull-right"><i id="icon" class="fa fa-plus" aria-hidden="true"></i></span></a>
</h4>
</div>

<div id="collapse2" class="panel-collapse collapse">
<div class="panel-body">
<p>Lorem ipsum dolor sit amet, </p>
</div>
</div>
</div>

<div class="panel">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse3">Goals
<span class="pull-right"><i id="icon" class="fa fa-plus" aria-hidden="true"></i></span></a>
</h4>
</div>

<div id="collapse3" class="panel-collapse collapse">
<div class="panel-body">
<p>Lorem ipsum dolor sit amet,</p>
</div>
</div>
</div>



</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>

</div>

最佳答案

您必须切换折叠以打开面板,并在隐藏模态时折叠所有可折叠组件。

$('#myModal').on('hidden.bs.modal', function () {
$('.collapse').collapse('hide');
})
$('#myModal').on('shown.bs.modal', function () {
$('#collapse1').collapse('show');
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Bootstrap Demo</title>
</head>
<body>
<button id="modalButtton" type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 id="modal-title"> </h4>
</div>
<div class="modal-body">
<div class="panel-group" id="accordion">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse1">Course Introduction

<span class="pull-right"><i id="icon0" class="fa fa-plus open" aria-hidden="true"></i></span>
</a>

</h4>
</div>

<div id="collapse1" class="panel-collapse collapse in">
<div class="panel-body">
<p> abc abc abc </p>
</div>
</div>
</div>


<div class="panel ">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse2">Description
<span class="pull-right"><i id="icon" class="fa fa-plus" aria-hidden="true"></i></span></a>
</h4>
</div>

<div id="collapse2" class="panel-collapse collapse">
<div class="panel-body">
<p>Lorem ipsum dolor sit amet, </p>
</div>
</div>
</div>

<div class="panel">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse3">Goals
<span class="pull-right"><i id="icon" class="fa fa-plus" aria-hidden="true"></i></span></a>
</h4>
</div>

<div id="collapse3" class="panel-collapse collapse">
<div class="panel-body">
<p>Lorem ipsum dolor sit amet,</p>
</div>
</div>
</div>



</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>

</div>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

关于javascript - 当模态被解除或关闭时如何关闭所有可折叠的 div( Accordion )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41642263/

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