简体   繁体   English

Bootstrap'collapsed'类未出现在DOM加载中

[英]Bootstrap 'collapsed' class not appearing on DOM load

I've got an accordion menu using Bootstrap's Collapse functionality, and I tried to style it based on the CSS classes Bootstrap uses to show and hide the collapsible element. 我有一个使用Bootstrap的Collapse功能的手风琴菜单,我尝试根据Bootstrap用于显示和隐藏可折叠元素的CSS类对它进行样式设置。

My problem is that when the document is fully loaded, the collapsed class is not loaded on the collapsed elements, while they appear collapsed. 我的问题是,当文档完全加载时, collapsed类不会显示在折叠的元素上,而collapsed类却不会加载。 So my page renders with it's 'open' styling, while every collapsible element is collapsed like it should be. 因此,我的页面使用“开放”样式进行渲染,而每个可折叠元素都按应有的方式折叠。 After the first couple of clicks, the collapsed class is present and the styling works as it should. 单击前两次后,将出现collapsed类,并且样式将按预期工作。

Forcing the collapsed class onto each element post DOM load for one is a messy way to solve the problem, but the styling still shows up for a second before changing. 在DOM加载后强制将collapsed类加载到每个元素上是解决问题的一种麻烦方法,但是样式在更改之前仍然会显示一秒钟。

To illustrate, this should be happening: 为了说明这一点,应该正在发生:

Collapsed State 崩溃状态 崩溃状态

Uncollapsed State 崩溃状态 崩溃状态

But instead, on load, I'm getting this as the collapsed classes simply aren't present: 但是,相反,在加载时,由于collapsed类根本不存在,我得到了这个: 负载实际发生了什么

Any ideas what can be done about this? 有什么办法可以解决吗?

Edit: Code Inclusion (ignore the Razor snippets) 编辑:代码包含(忽略剃刀片段)

<button class="category-select" data-toggle="collapse" data-target="#collapsible-@i">@fieldset.GetValue("title")</button>
    <div id="collapsible-@i" class="collapse">
        <div class="category-inner">
            @Html.Raw(@fieldset.GetValue("innerText"))
        </div>
    </div>

Just realised what I did wrong based on the styles that are present. 刚刚意识到我根据存在的样式做错了什么。 What I should have done is added the collapsed class to the html myself. 我应该做的是自己将collapsed类添加到html中。

The code should in fact look like this: 该代码实际上应如下所示:

<button class="category-select collapsed" data-toggle="collapse" data-target="#collapsible-@i">@fieldset.GetValue("title")</button>
<div id="collapsible-@i" class="collapse">
    <div class="category-inner">
        @Html.Raw(@fieldset.GetValue("innerText"))
    </div>
</div>

Correct styles are then applied on load, ready for collapsed to be removed on click. 然后,将正确的样式应用于负载,准备collapsed可以将其单击删除。

Very silly mistake. 非常愚蠢的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM