Retain Twitter Bootstrap Collapse state on Page refresh/Navigation

后端 未结 6 1398
死守一世寂寞
死守一世寂寞 2020-11-27 16:19

I\'m using Bootstrap \"collapse\" plugin to make an accordion for a long list of links. The accordion-body tag includes \"collapse\" so all the groups are collapsed when the

6条回答
  •  鱼传尺愫
    2020-11-27 17:01

    Another option available is to use the url hash.

    $(document).ready(function () {
        var hash = window.location.hash;
        if (hash) {
            $("#accordion .panel-collapse").removeClass('in');
            $(hash).addClass('in');
        }
    
        $('#accordion').on('shown.bs.collapse', function () {
            var activeId = $("#accordion .in").attr('id');
            window.location.hash = activeId;
        });
    
        $('#accordion').on('hidden.bs.collapse', function () {
            window.location.hash = '';
        });
    });
    

提交回复
热议问题