本文介绍了Bootstrap在加载时折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个脚本

$(function () {
$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
$('.tree li.parent_li > span').on('click', function (e) {
    var children = $(this).parent('li.parent_li').find(' > ul > li');
    if (children.is(":visible")) {
        children.hide('fast');
        $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
    } else {
        children.show('fast');
        $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
    }
    e.stopPropagation();
});

用于递归可折叠菜单。它工作得很好。但是我需要最初折叠的菜单,即页面加载和仅在点击时展开。

for recursive collapsible menu. It works perfectly. But I need the menu collapsed initially that is when the page load and expand only on click.

我的JS知识很弱。但我尝试使用toggle();并隐藏(); ,它导致折叠并且在点击时不会扩展

My JS knowledge is weak. But i tried using toggle(); and hide(); , it results in collapsed and doesnt expand on click

下面是递归的PHP代码

Below is recursive php code

<?php
function listroles($roles)
{
    ?>
    <ul>
    <?php
    foreach($roles as $role)
    {
        ?>
        <li>
            <span><i class="icon-plus "></i> Parent</span> <a href="<?php echo $role['category']->slug; ?>"><?php echo $role['category']->name; ?></a>
            <?php
            if ( ! empty($role['children']))
            {
                listroles($role['children']);
            }
            ?>
        </li>
        <?php
    }
    ?>
    </ul>
    <?php
}

listroles($this->categories_menu);
?> 


推荐答案

你可以添加一条css规则来隐藏孩子李元素开始

You can add a css rule to hide the child li elements at the start

.tree li ul > li {
    display: none;
}

演示:

或隐藏页面加载时的子li元素

or hide the child li element on page load

$(function () {
    $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');

    //hide the child li elements
    $('.tree li ul > li').hide();
    $('.tree li.parent_li > span').on('click', function (e) {
        var children = $(this).parent('li.parent_li').find(' > ul > li');
        if (children.is(":visible")) {
            children.hide('fast');
            $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
        } else {
            children.show('fast');
            $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
        }
        e.stopPropagation();
    });
});

演示:

这篇关于Bootstrap在加载时折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:25