Bootstrap 5 的 getOrCreateInstance() 方法为折叠项立即切换元素吗?

huangapple go评论62阅读模式
英文:

Why does Bootstrap 5's getOrCreateInstance() method for collapsibles immediately toggle the element?

问题

我到现在才开始使用Bootstrap 5中的Collapse功能,但我非常不喜欢它!

如果我有一个简单的设置,有一个可折叠元素 - 最初是隐藏的 - 和一个切换其可见性的按钮,就像这样:

<button class="btn btn-primary" id="toggleDetails">Show Details</button>
<div class="collapse border mt-2 p-2" id="details">
    这里有一些信息。
</div>

还有一些类似这样的JavaScript:

const details = document.getElementById("details");
const detailsCollapse = bootstrap.Collapse.getOrCreateInstance(details);

const btn = document.getElementById("toggleDetails");
btn.addEventListener("click", (e) => {
    detailsCollapse.toggle();
});

为什么getOrCreateInstance会切换可折叠元素的可见性?它的唯一工作肯定是获取折叠实例。这真的让人气愤。我做错了什么吗?如果没有,我怎样才能获得折叠实例而不切换它?显然,在我的实际代码中,设置比这复杂,但这个问题目前让我停顿不前。

这里有一个演示这个问题的fiddle

英文:

I've not had to work with the Collapse feature in Bootstrap 5 until today and I strongly dislike it!

If I have a simple setup with a collapsible element - hidden initially - and a button to toggle its visibility, like this:

&lt;button class=&quot;btn btn-primary&quot; id=&quot;toggleDetails&quot;&gt;Show Details&lt;/button&gt;
&lt;div class=&quot;collapse border mt-2 p-2&quot; id=&quot;details&quot;&gt;
    Here is some information.
&lt;/div&gt;

and some Javascript like this:

const details = document.getElementById(&quot;details&quot;);
const detailsCollapse = bootstrap.Collapse.getOrCreateInstance(details);

const btn = document.getElementById(&quot;toggleDetails&quot;);
btn.addEventListener(&quot;click&quot;, (e) =&gt; {
	detailsCollapse.toggle();
});

why does the getOrCreateInstance toggle the collapsible element's visibility? Surely its only job is to get the collapse instance. It's absolutely infuriating. Am I doing something wrong? If not, how can I get the collapse instance without toggling it? Obviously in my actual code the setup is more complex than this but this issue is currently holding me up.

There's a fiddle showing the problem here.

答案1

得分: 1

添加 toggle: false 选项

默认行为是

> 在调用时切换可折叠元素。

<!-- 开始片段:js 隐藏:false 控制台:true babel: false -->

<!-- 语言: lang-js -->

const details = document.getElementById(&quot;details&quot;);
const detailsCollapse = bootstrap.Collapse.getOrCreateInstance(details, {
  toggle: false
})


const btn = document.getElementById(&quot;toggleDetails&quot;);

btn.addEventListener(&quot;click&quot;, (e) =&gt; {
  detailsCollapse.toggle();
});

<!-- 语言: lang-html -->

    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;
    
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    
&lt;button class=&quot;btn btn-primary&quot; id=&quot;toggleDetails&quot;&gt;Show Details&lt;/button&gt;
&lt;div class=&quot;collapse border mt-2 p-2&quot; id=&quot;details&quot;&gt;
    这里有一些信息。
&lt;/div&gt;

<!-- 结束片段 -->

英文:

Add toggle: false option

The default behavior is

> Toggles the collapsible element on invocation.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const details = document.getElementById(&quot;details&quot;);
const detailsCollapse = bootstrap.Collapse.getOrCreateInstance(details, {
  toggle: false
})


const btn = document.getElementById(&quot;toggleDetails&quot;);

btn.addEventListener(&quot;click&quot;, (e) =&gt; {
  detailsCollapse.toggle();
});

<!-- language: lang-html -->

    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;
    
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    
&lt;button class=&quot;btn btn-primary&quot; id=&quot;toggleDetails&quot;&gt;Show Details&lt;/button&gt;
&lt;div class=&quot;collapse border mt-2 p-2&quot; id=&quot;details&quot;&gt;
    Here is some information.
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月6日 19:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定