gpt4 book ai didi

javascript - 如何在重新加载 Bootstrap 5 时保存折叠状态

转载 作者:行者123 更新时间:2023-12-05 06:05:51 24 4
gpt4 key购买 nike

我是 bootstrap 和 javascript 的新手。如何保存折叠状态,以便状态保持页面刷新?

<p>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Collapse Button
</button>
</p>
<div class="collapse" id="collapseExample">
<div class="card card-body">
Makers Academy engineering project has commenced. We have had a great experience so far with rails and now feel like we are getting to grips with the more complicated aspects. This guide is a quick introduction into adding likes to posts.
</div>
</div>

我的 Javascript 尝试不起作用。

$(document).ready(function () {
$(".collapse").on("shown.bs.collapse", function () {
localStorage.setItem("coll_" + this.id, true);
});

$(".collapse").on("hidden.bs.collapse", function () {
localStorage.removeItem("coll_" + this.id);
});

$(".collapse").each(function () {
if (localStorage.getItem("coll_" + this.id) === "true") {
$(this).collapse("show");
}
else {
$(this).collapse("hide");
}
});
});

最佳答案

它确实有效 - 请参阅 this fiddle .

我看到您正在使用 $ 符号 - 所以您也需要加载 jQuery。确保它在 bootstrap.js 之前加载。

关于javascript - 如何在重新加载 Bootstrap 5 时保存折叠状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65941994/

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