简体   繁体   English

Bootstrap 4:如何显示一个可折叠项目并隐藏其他项目?

[英]Bootstrap 4: How to show one collapsible item and hide others?

I have some collapsible menus and I want to hide others when one clicked.我有一些可折叠的菜单,我想在单击时隐藏其他菜单。 Please note I want to completely hide them, not collapse them.请注意,我想完全隐藏它们,而不是折叠它们。 It's different from accordion behavior.它与手风琴的行为不同。

I used accordion code and added the following function:我使用手风琴代码并添加了以下 function:

$('#bottomNavBar .collapse').on('show.bs.collapse', function (e) {
    // do something…
    var me = e.target;
    $('#bottomNavBar [aria-expanded="false"]').each(function () {
        if (!$(this).is(me)) {
            $(this).hide();
        }
    });
});

The problem is that the item which has been clicked to expand is also hides because its aria-expanded is false (however its sub-menus appears).问题是被点击展开的项目也被隐藏了,因为它的aria-expanded是假的(但是它的子菜单出现了)。 I didn't want to use shown.bs.collapse event because of my visual plans.由于我的视觉计划,我不想使用shown.bs.collapse事件。 I tried to compare each item with the calling object but it still doesn't work.我试图将每个项目与调用 object 进行比较,但它仍然不起作用。

This function solved my problem, I just need click event.这个 function 解决了我的问题,我只需要click事件。

$('#bottomNavBar [aria-expanded="false"]').on('click', function (e) {
    // do something…
    var me = this;
    $('#bottomNavBar [aria-expanded="false"]').each(function () {
        if (this != me) {
            $(this).hide();
        }
    });
});

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

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