gpt4 book ai didi

javascript - 检查所有 Bootstrap Accordion 是否打开并执行某些操作

转载 作者:行者123 更新时间:2023-12-02 21:27:16 25 4
gpt4 key购买 nike

我有一个 Bootstrap Accordion ,还有一个“全部打开”、“全部关闭”按钮,效果很好,但我想做的是当 Accordion 全部手动打开,而不是通过“全部打开”、“全部关闭”按钮打开时按钮更改为“全部关闭”。有没有办法检查所有 Accordion 是否都打开,以便我可以添加按钮功能?我知道您可以使用 shown.bs.collapse 和 hide.bs.collapse 来查看 Accordion 是否打开,但是您如何检查所有内容是否都打开?

<!-- panel -->
<div class="panel accordion-toggle" href="#panelOne" data-toggle="collapse">
<div class="panel-heading">
<div class="l-col-panel">
<div class="panel-title__wrapper">
<div class="accordion__text">
<!--panel title -->
<h3 class="title-3">Title</h3>
</div>
</div>
<div class="accordion-toggle__angle-icon">
<!-- angle icon -->
<i class="accordion__angle fa fa-angle-down"></i>
</div>
</div>
<!-- /.l-col-panel -->
</div>
<div id="panelOne" class="panel-collapse collapse">
<div class="panel-body">

<div class="panel-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, voluptate!</p>
</div>
</div>
</div>
</div>
<!--end: panel -->

<!-- panel -->
<div class="panel accordion-toggle" href="#panelOne" data-toggle="collapse">
<div class="panel-heading">
<div class="l-col-panel">
<div class="panel-title__wrapper">
<div class="accordion__text">
<!--panel title -->
<h3 class="title-3">Title</h3>
</div>
</div>
<div class="accordion-toggle__angle-icon">
<!-- angle icon -->
<i class="accordion__angle fa fa-angle-down"></i>
</div>
</div>
<!-- /.l-col-panel -->
</div>
<div id="panelTwo" class="panel-collapse collapse">
<div class="panel-body">

<div class="panel-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, voluptate!</p>
</div>
</div>
</div>
</div>
<!--end: panel -->

<!-- panel -->
<div class="panel accordion-toggle" href="#panelOne" data-toggle="collapse">
<div class="panel-heading">
<div class="l-col-panel">
<div class="panel-title__wrapper">
<div class="accordion__text">
<!--panel title -->
<h3 class="title-3">Title</h3>
</div>
</div>
<div class="accordion-toggle__angle-icon">
<!-- angle icon -->
<i class="accordion__angle fa fa-angle-down"></i>
</div>
</div>
<!-- /.l-col-panel -->
</div>
<div id="panelThree" class="panel-collapse collapse">
<div class="panel-body">

<div class="panel-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, voluptate!</p>
</div>
</div>
</div>
</div>
<!--end: panel -->

jQuery:

  $('.panel-collapse').on('shown.bs.collapse', function () {
console.log('OPEN')
});

$('.panel-collapse').on('hidden.bs.collapse', function () {
console.log('CLOSED')
});

最佳答案

$('#panelOne').on('shown.bs.collapse', function () {
let itemsList = document.getElementsByClassName('panel-collapse');
let isOpen = [];
for (let item of itemsList) {
if (item.classList.contains('in')) {
isOpen.push(true)
} else {
isOpen.push(false)
}
}

let allOpen = !isOpen.includes(false)
console.log(allOpen)
})
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>

<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- panel -->
<div class="panel accordion-toggle" href="#panelOne" data-toggle="collapse">
<div class="panel-heading">
<div class="l-col-panel">
<div class="panel-title__wrapper">
<div class="accordion__text">
<!--panel title -->
<h3 class="title-3">Title</h3>
</div>
</div>
<div class="accordion-toggle__angle-icon">
<!-- angle icon -->
<i class="accordion__angle fa fa-angle-down"></i>
</div>
</div>
<!-- /.l-col-panel -->
</div>
<div id="panelOne" class="panel-collapse collapse">
<div class="panel-body">

<div class="panel-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, voluptate!</p>
</div>
</div>
</div>
</div>
<!--end: panel -->

<!-- panel -->
<div class="panel accordion-toggle" href="#panelTwo" data-toggle="collapse">
<div class="panel-heading">
<div class="l-col-panel">
<div class="panel-title__wrapper">
<div class="accordion__text">
<!--panel title -->
<h3 class="title-3">Title</h3>
</div>
</div>
<div class="accordion-toggle__angle-icon">
<!-- angle icon -->
<i class="accordion__angle fa fa-angle-down"></i>
</div>
</div>
<!-- /.l-col-panel -->
</div>
<div id="panelTwo" class="panel-collapse collapse">
<div class="panel-body">

<div class="panel-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, voluptate!</p>
</div>
</div>
</div>
</div>
<!--end: panel -->

<!-- panel -->
<div class="panel accordion-toggle" href="#panelThree" data-toggle="collapse">
<div class="panel-heading">
<div class="l-col-panel">
<div class="panel-title__wrapper">
<div class="accordion__text">
<!--panel title -->
<h3 class="title-3">Title</h3>
</div>
</div>
<div class="accordion-toggle__angle-icon">
<!-- angle icon -->
<i class="accordion__angle fa fa-angle-down"></i>
</div>
</div>
<!-- /.l-col-panel -->
</div>
<div id="panelThree" class="panel-collapse collapse">
<div class="panel-body">

<div class="panel-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, voluptate!</p>
</div>
</div>
</div>
</div>
<!--end: panel -->

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</body>
</html>

您可以检查in中的类是否存在。

关于javascript - 检查所有 Bootstrap Accordion 是否打开并执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60707911/

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