Bootstrap Collapse - open the given id fragment

后端 未结 4 1973
温柔的废话
温柔的废话 2020-12-16 12:14

Imagine a Bootstrap collapse with 3 parts

...
相关标签:
4条回答
  • 2020-12-16 12:28
    $("#accordionTwo").collapse('show');
    

    To open the given HTTP fragment identifier, try this:

    $(document).ready(function() {
        var anchor = window.location.hash;
        $(".collapse").collapse('hide');
        $(anchor).collapse('show');
    });
    

    EDIT:

    As pointed by bart in the comments: be careful with targeting .collapse because this class is also used for the navigation bar when the viewport is xs.

    0 讨论(0)
  • 2020-12-16 12:29

    For really simple and quick to implement hash routing, you could try something like Routie

    routie({
        accordionOne: function() {
            $('#accordionOne').collapse('show');
        },
        accordionTwo: function() {
            $('#accordionTwo').collapse('show');
        },
        accordionThree: function() {
            $('#accordionThree').collapse('show');
        }
    });
    
    0 讨论(0)
  • 2020-12-16 12:39

    Yet another solution, a bit smaller and compact:

    $(document).ready(function() {
      var anchor = window.location.hash;
      $(anchor).collapse('toggle');
    });
    
    0 讨论(0)
  • 2020-12-16 12:41

    This line will open the correct hash

    location.hash && $(location.hash + '.collapse').collapse('show');
    
    0 讨论(0)
提交回复
热议问题