gpt4 book ai didi

javascript - 如何让 Accordion 默认不展开?

转载 作者:太空宇宙 更新时间:2023-11-04 03:24:09 26 4
gpt4 key购买 nike

如何让 Accordion 默认不展开?当用户点击它时展开它。

然后,展开后,我想将标题从“显示”切换为“隐藏”。

这是我到目前为止得到的结果。

FIDDLE

HTML

<div class="accordion" id="accordion1">
<div class="accordion-group">
<div class="accordion-heading">

<a class="accordion-toggle" data-toggle="collapse" href="#collapseOne">
Show
</a>
</div>
<div id="collapseOne" class="accordion-body collapse">
<div class="accordion-inner">
In software, a stack overflow occurs when the stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash.
</div>
</div>
</div>
</div>

JS

    <script type="text/javascript">

$(function(){
$(window).resize(function() { // Optional: if you want to detect when the window is resized;
processBodies($(window).width());
});
function processBodies(width) {
if(width > 768) {
$('.accordion-body').collapse('show');
}
else {
$('.accordion-body').collapse('hide');
}
}
processBodies($(window).width());
});




</script>

最佳答案

要默认折叠 Accordion ,请在其上使用 in 类。

<div id="collapseOne" class="accordion-body collapse in">
<!-- ... here it is .............................^^ -->
</div>

要更改可以打开/关闭它的文本,只需添加 shownhidden 的事件监听器,并根据发生的事件更新文本此刻。

$(document).ready(function(){    
$('#collapseOne').on('shown', function () {
$("#showHide").text("Hide");
});

$('#collapseOne').on('hidden', function () {
$("#showHide").text("Show");
});
});

这是您更新的 Fiddle:http://jsfiddle.net/z2tNg/49/

关于javascript - 如何让 Accordion 默认不展开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27385691/

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