简体   繁体   English

Jquery 手风琴在页面加载时折叠

[英]Jquery accordion collapsed on page load

I am trying to create an expandable accordion using jquery.我正在尝试使用 jquery 创建一个可扩展的手风琴。 I am not able to figure out that how am I supposed to prevent it from expanding on page load.我无法弄清楚我应该如何防止它在页面加载时扩展。 I have no idea of jquery,and any help will be greatly appreciated.我不知道 jquery,任何帮助将不胜感激。

            <ul>
            <li class="expandable root">

                <label for=" Oraganization" class="active"> Oraganization</label> 
                <span class="pull-right oraganization active expand-icon glyphicon glyphicon-minus"></span> 

                <ul id="accordion">
                    <li class="expandable">
                        <span class="expand-icon active glyphicon glyphicon-minus"></span> 
                        <input type="checkbox" id="Manager-1"> 
                        <label for="Manager-1"> Manager-1</label> 

                        <ul id="accordion">
                            <li>
                                <input type="checkbox" id="Sub-Manager-1"> 
                                <label for="Sub-Manager-1"> Sub-Manager-1</label> 
                            </li>
                            <li class="expandable">
                                <span class="expand-icon active glyphicon glyphicon-minus"></span> 
                                <input type="checkbox" id="Sub-Manager-2"> 
                                <label for="Sub-Manager-2"> Sub-Manager-2</label> 

                                <ul class="accordion">
                                    <li>
                                        <input type="checkbox" id="Associate-1"> 
                                        <label for="Associate-1"> Associate-1</label> 
                                    </li>
                                    <li>
                                        <input type="checkbox" id="Associate-2"> 
                                        <label for="Associate-2"> Associate-2</label> 
                                    </li>



                            <li class="expandable">
                                <span class="expand-icon active glyphicon glyphicon-minus"></span> 
                                <input type="checkbox" id="Associate-5"> 
                                <label for="Associate-5"> Associate-5</label> 
                                <ul class="accordion">
                                    <li>
                                        <input type="checkbox" id="Sub-associate-1"> 
                                        <label for="Sub-associate-1"> Sub-associate-1</label> 
                                    </li>
                                    <li>
                                        <input type="checkbox" id="Sub-associate-2"> 
                                        <label for="Sub-associate-2"> Sub-associate-2</label> 
                                    </li>
                                    <li>
                                        <input type="checkbox" id="Sub-associate-3"> 
                                        <label for="Sub-associate-3"> Sub-associate-3</label> 
                                    </li>   
                                    <li>
                                        <input type="checkbox" id="Sub-associate-4"> 
                                        <label for="Sub-associate-4"> Sub-associate-4</label> 
                                    </li>
                                </ul>
                                </li>
                                </ul>
                                </li>
                            </li>
                        </ul>
                    </li>


                    <li class="expandable">
                                <span class="expand-icon active glyphicon glyphicon-plus"></span> 
                                <input type="checkbox" id="Manager-2"> 
                                <label for="Manager-2"> Manager-2</label> 
                    </li>
                    <li class="expandable">

                                <span class="expand-icon active glyphicon glyphicon-plus"></span> 
                                <input type="checkbox" id="Manager-3"> 
                                <label for="Manager-3"> Manager-3</label> 
                    </li>                       

                </ul>
            </li>
        </ul>

JS File: JS文件:

$(document).ready(function(){

$('.expand-icon').click(function(){

    var elem = $(this);
    if(elem.hasClass('active')) {
        elem.removeClass('active');

        var subElem = elem.siblings('ul');
        var nestedElem =elem.siblings('ul').find('ul');

        if(nestedElem.length == 0) {
            subElem.slideUp('fast');
        }
        else {
            nestedElem.slideUp('fast',function(){
                subElem.slideUp('fast');
            });
        }
        $.when(elem.removeClass('glyphicon-minus')).then(function(){
            elem.addClass('glyphicon-plus');
        });
    }
    else {
        elem.addClass('active');
        elem.siblings('ul').slideDown('fast',function(){
            elem.siblings('ul').find('ul').slideDown('fast');   
        });
        $.when(elem.removeClass('glyphicon-plus')).then(function(){
            elem.addClass('glyphicon-minus');
        });
    }

});


$('.expandable :checkbox').on('change', function() {

    $(this).parent().find('li input[type=checkbox]').prop('checked', $(this).is(':checked'));
    var sibs = false;
    $(this).closest('ul').children('li').each(function () {
        if($('input[type=checkbox]', this).is(':checked')) sibs=true;
    });
    $(this).parents('ul').siblings(':checkbox').prop('checked', sibs);

});

}); });

You an find the bootply at this link: http://www.bootply.com/1W8bSTnmx6您可以在此链接中找到 bootply: http : //www.bootply.com/1W8bSTnmx6

Fiddle Here在这里小提琴

This is because you have this first accordian open when loading it.这是因为您在加载时打开了第一个手风琴。 To fix it, do a click operation on this at the time of load with $($('#accordion .expand-icon')[0]).click();要修复它,请在加载时使用$($('#accordion .expand-icon')[0]).click(); . . Check the working example here http://www.bootply.com/vGoKbvfKtl检查这里的工作示例http://www.bootply.com/vGoKbvfKtl

you are just displaying all the accordions when the page gets loaded since there is no hiding any where.您只是在页面加载时显示所有手风琴,因为没有隐藏任何地方。

According to your code Since only the first Accordion contains the data, it is getting displayed else every expandable div will get displayed.根据您的代码,由于只有第一个 Accordion 包含数据,所以它会被显示,否则每个可扩展的 div 都会被显示。

The solution for this is to add a hide class to the class accordion so that it does not display at the beginning, the at the time of click function, you can remove that class and it gets displayed.对此的解决方案是在类手风琴中添加一个隐藏类,使其在开始时不显示,在单击功能时,您可以删除该类并显示它。

 $(document).ready(function(){ $('.expand-icon').click(function(){ $('ul').removeClass('hide') var elem = $(this); if(elem.hasClass('active')) { elem.removeClass('active'); var subElem = elem.siblings('ul'); var nestedElem =elem.siblings('ul').find('ul'); if(nestedElem.length == 0) { subElem.slideUp('fast'); } else { nestedElem.slideUp('fast',function(){ subElem.slideUp('fast'); }); } $.when(elem.removeClass('glyphicon-minus')).then(function(){ elem.addClass('glyphicon-plus'); }); } else { elem.addClass('active'); elem.siblings('ul').slideDown('fast',function(){ elem.siblings('ul').find('ul').slideDown('fast'); }); $.when(elem.removeClass('glyphicon-plus')).then(function(){ elem.addClass('glyphicon-minus'); }); } }); $('.expandable :checkbox').on('change', function() { $(this).parent().find('li input[type=checkbox]').prop('checked', $(this).is(':checked')); var sibs = false; $(this).closest('ul').children('li').each(function () { if($('input[type=checkbox]', this).is(':checked')) sibs=true; }); $(this).parents('ul').siblings(':checkbox').prop('checked', sibs); }); $('.organization').click(function(){ $('.accordion')[0].click() }) });
 <ul> <li class="expandable "> <label for=" Oraganization" class="active"> Oraganization</label> <span class=" organization active expand-icon glyphicon glyphicon-minus"></span> <ul id=""> <li class="expandable"> <span class="expand-icon glyphicon glyphicon-plus"></span> <input type="checkbox" id="Manager-1"> <label for="Manager-1"> Manager-1</label> <ul class="accordion hide"> <li> <input type="checkbox" id="Sub-Manager-1"> <label for="Sub-Manager-1"> Sub-Manager-1</label> </li> <li class="expandable"> <span class="expand-icon active glyphicon glyphicon-minus"></span> <input type="checkbox" id="Sub-Manager-2"> <label for="Sub-Manager-2"> Sub-Manager-2</label> <ul class="accordion"> <li> <input type="checkbox" id="Associate-1"> <label for="Associate-1"> Associate-1</label> </li> <li> <input type="checkbox" id="Associate-2"> <label for="Associate-2"> Associate-2</label> </li> <li class="expandable"> <span class="expand-icon active glyphicon glyphicon-minus"></span> <input type="checkbox" id="Associate-5"> <label for="Associate-5"> Associate-5</label> <ul class="accordion"> <li> <input type="checkbox" id="Sub-associate-1"> <label for="Sub-associate-1"> Sub-associate-1</label> </li> <li> <input type="checkbox" id="Sub-associate-2"> <label for="Sub-associate-2"> Sub-associate-2</label> </li> <li> <input type="checkbox" id="Sub-associate-3"> <label for="Sub-associate-3"> Sub-associate-3</label> </li> <li> <input type="checkbox" id="Sub-associate-4"> <label for="Sub-associate-4"> Sub-associate-4</label> </li> </ul> </li> </ul> </li> </ul> </li> <li class="expandable"> <span class="expand-icon active glyphicon glyphicon-plus"></span> <input type="checkbox" id="Manager-2"> <label for="Manager-2"> Manager-2</label> </li> <li class="expandable"> <span class="expand-icon active glyphicon glyphicon-plus"></span> <input type="checkbox" id="Manager-3"> <label for="Manager-3"> Manager-3</label> </li> </ul> </li> </ul>

Here is the bootply, Bootply link for it这是 bootply,它的 Bootply 链接

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

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