jquery accordion collapsed by default on page load

前端 未结 6 1556
深忆病人
深忆病人 2020-12-20 21:23

I am using JQuery UI accordion in my page. I have following Javascript code on my page load:

$(function() {
    $(\"#accordion\").accordion({
            act         


        
相关标签:
6条回答
  • 2020-12-20 21:41

    // We can also use the below code to collapse accordian on the page load and it should use when we are using bootstrap 2.0

        $(document).ready(function () {
              if ($("#accordianId").length>0) {
                            $("#accordianId").trigger("click");
                        }
                 });
    

    Other wise we should use below code for bootstrap 3.0

    $( "#accordianId" ).accordion( "option", "active", 0 );
    
    0 讨论(0)
  • 2020-12-20 21:42
    $(document).ready(function() {
       $('.collapse').collapse({
         toggle: false
       });
    });
    

    This will set all .collapse classes in DOM to close, but only once the DOM has finished loading.

    0 讨论(0)
  • 2020-12-20 21:54

    For me this works:

    $(function() {
        $( "#accordion" ).accordion({
                collapsible: true,
                autoHeight: true,
                active: false
    
            });
    });
    
    0 讨论(0)
  • 2020-12-20 21:57

    Although not a direct answer, maybe you can render it hidden and then show it when its created:

    $("#accordion").accordion({
       active: false,            
       autoHeight: false,            
       navigation: true,            
       collapsible: true,
       create: function(event, ui) { $("#accordion").show(); }
    });
    

    Update: This fiddle works for me: http://jsfiddle.net/47aSC/6/

    0 讨论(0)
  • 2020-12-20 22:03

    The best solution is:

    open jquery.ui.accordion.js and edit lines 29 and 31 (by the way I'm using 1.10.4).

    Edit line 29 to Active: 100, Edit line 31 to collapsible: true,

    This way you don't need to write any script or function in the header of the page. By setting Active to a high number (for example 100) you are saying that 100th h3 tag is active (which basically doesn't exist).

    The collapsible: true says that all open h3 tags are collapsible.

    It solves the problem completely.

    0 讨论(0)
  • 2020-12-20 22:04

    It's probably loading something near the end of the page slowly. If you can't fix that, you could try declaring the element having display:none applied to it in css, then:

    $("#accordion").show().accordion({
            active: false,
            autoHeight: false,
            navigation: true,
            collapsible: true
        });
    

    There could be a cleaner way of doing that (as @Mrchief suggests), but I don't think .accordion() formats hidden elements nicely. You'll have to test.

    0 讨论(0)
提交回复
热议问题