如何防止 Bootstrap 崩溃根据条件显示? bs5
How to prevent Bootstrap collapse from show based on condition? bs5
我需要阻止 collapsible 面板在特定值为 null 时显示或打开。这看起来很容易,但我尝试了很多次都没有成功。
HTML
<a href="#" data-bs-toggle="collapse" data-bs-target="#infoBox" aria-expanded="false" aria-controls="infoBox"></i>
<div class="collapse m-3" id="infoBox">some ifos ...</div>
第一次尝试:
var someVal;
var infoBox = document.getElementById('infoBox')
infoBox.addEventListener('show.bs.collapse', function () {
// console.log(e.target);
if (someVal == null) return;
})
第二次尝试:
var someVal;
var infoBox = document.getElementById('infoBox');
var bsCollapse = new bootstrap.Collapse(infoBox, {
show: false,
dispose: true
})
infoBox.addEventListener('show.bs.collapse', function (e) {
// console.log(e.target);
if (someVal == null) return bsCollapse;
})
None 个正在工作
您必须使用 e.preventDefault()
功能来防止默认。
var toggle = false;
$("#wrapper").on('show.bs.collapse', function (e) {
if (!toggle) {
e.preventDefault();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div id="wrapper">
<a href="#" data-bs-toggle="collapse" data-bs-target="#infoBox" aria-expanded="false" aria-controls="infoBox" id="myCollapsible">press me</a>
<div class="collapse m-3" id="infoBox">some ifos ...</div>
</div>
我需要阻止 collapsible 面板在特定值为 null 时显示或打开。这看起来很容易,但我尝试了很多次都没有成功。
HTML
<a href="#" data-bs-toggle="collapse" data-bs-target="#infoBox" aria-expanded="false" aria-controls="infoBox"></i>
<div class="collapse m-3" id="infoBox">some ifos ...</div>
第一次尝试:
var someVal;
var infoBox = document.getElementById('infoBox')
infoBox.addEventListener('show.bs.collapse', function () {
// console.log(e.target);
if (someVal == null) return;
})
第二次尝试:
var someVal;
var infoBox = document.getElementById('infoBox');
var bsCollapse = new bootstrap.Collapse(infoBox, {
show: false,
dispose: true
})
infoBox.addEventListener('show.bs.collapse', function (e) {
// console.log(e.target);
if (someVal == null) return bsCollapse;
})
None 个正在工作
您必须使用 e.preventDefault()
功能来防止默认。
var toggle = false;
$("#wrapper").on('show.bs.collapse', function (e) {
if (!toggle) {
e.preventDefault();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div id="wrapper">
<a href="#" data-bs-toggle="collapse" data-bs-target="#infoBox" aria-expanded="false" aria-controls="infoBox" id="myCollapsible">press me</a>
<div class="collapse m-3" id="infoBox">some ifos ...</div>
</div>