简体   繁体   English

如何使子下拉菜单显示为默认(折叠)结构

[英]How can I make the sub drop-down appear in default (collapsed) structure

I have a drop down list with multi drop downs, which opens and closes on alternative clicks but when I try to reopen the drop down the entire drops appear without closing. 我有一个包含多个下拉列表的下拉列表,该列表会在其他点击时打开和关闭,但是当我尝试重新打开下拉列表时,整个下拉列表都会显示而不关闭。 How can i make it appear on default structure. 我如何使它出现在默认结构上。 please help guys. 请帮助大家。 Thanks in advance 提前致谢

<style>
.closed{display:none;}
.opened{display:block;}
</style>
<div class="dropdown">
<p class=" dropdown-toggle" type="" data-toggle="dropdown" 
style="padding: 15px 0px;font-size: 24px;"><i class="fas fa-bars">
</i>&nbsp;CATEGORY</p>
<ul class="dropdown-menu">
    <li class="dropdown-submenu">
        <a class="test" tabindex="-1" href="#">New dropdown <span class="caret"></span></a>
        <ul class="dropdown-menu opened">
            <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
            <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
            <li class="dropdown-submenu">
                <a class="test" href="#">Another dropdown <span class="caret"></span></a>
                <ul class="dropdown-menu opened">
                    <li><a href="#">3rd level dropdown</a></li>
                    <li><a href="#">3rd level dropdown</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a tabindex="-1" href="#">HTML</a></li>
    <li><a tabindex="-1" href="#">CSS</a></li>
    <li class="dropdown-submenu">
        <a class="test" tabindex="-1" href="#">New dropdown <span class="caret"></span></a>
        <ul class="dropdown-menu closed">
            <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
            <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
            <li class="dropdown-submenu">
                <a class="test" href="#">Another dropdown <span class="caret"></span></a>
                <ul class="dropdown-menu closed">
                    <li><a href="#">3rd level dropdown</a></li>
                    <li><a href="#">3rd level dropdown</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
</div>

<script>
//new drop ends
$(function(){
$('.dropdown-submenu a.test').on("click", function(e){                  
    var ul = $(this).next('ul');
    if(ul.hasClass('opened')){
        ul.removeClass('opened').addClass('closed');
    }else{
        ul.addClass('opened').removeClass('closed');
    }
    if($(this).parents('ul').length == 1){
        $(this).parents('ul').find('ul').not(ul).addClass('closed').removeClass('opened')
    }

    e.stopPropagation();
    e.preventDefault();

});        
});  
  </script>

When you click the modified classes are: 单击修改后的类是:

在此处输入图片说明

The answer, remove "opened" class in dropdown-menus. 答案是,删除下拉菜单中的“打开”类。

        <ul class="dropdown-menu ">
            <li>
                <a tabindex="-1" href="#">2nd level dropdown</a>
            </li>
            <li>
                <a tabindex="-1" href="#">2nd level dropdown</a>
            </li>
            <li class="dropdown-submenu">
                <a class="test" href="#">Another dropdown
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu ">
                    <li>
                        <a href="#">3rd level dropdown</a>
                    </li>
                    <li>
                        <a href="#">3rd level dropdown</a>
                    </li>
                </ul>
            </li>
        </ul>

Replace jQuery code for: 将jQuery代码替换为:

    $('.dropdown').on('hidden.bs.dropdown', function () {
        $('.dropdown-submenu a.test').each(function () {
            var ul = $(this).next('ul');
            if (ul.hasClass('opened')) {
                ul.removeClass('opened').addClass('closed');
            } else {
                ul.addClass('opened').removeClass('closed');
            }
            if ($(this).parents('ul').length) {
                $(this).parents('ul').find('ul').not(ul).addClass('closed').removeClass('opened')
            }
            e.stopPropagation();
            e.preventDefault();
        });
    });

暂无
暂无

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

相关问题 如何使下拉菜单出现在幻灯片上? - How to make drop-down menu appear over slideshow? 从SuiteScript的下拉菜单中选择选项后,如何显示文本框? - How do I make a text box appear after selecting an option from a drop-down menu in SuiteScript? 如何使我的动态下拉列表也依赖于父下拉列表? - How can I make my dynamic drop-down list be dependent dependent also to parent drop down? 如何将我的选项设置为默认选项,因此如果我清除下拉菜单,默认情况下将填充该选项 - How to make my option a default so if I clear my drop-down it is populated by the default 默认情况下,在移动视图中下拉我的 boostrap 导航栏总是折叠的..我该如何解决这个问题? - Drop down my boostrap navbar in mobile view is always collapsed by default..how can i fix this? 如何在手机上隐藏下拉菜单的子链接? - How can I hide sub-links of drop-down menu on mobile? 如何根据下拉菜单选择从下拉菜单中选择后显示复选框列表? - How to make a checkbox list appear after a selection from a drop-down menu, depending on drop-down menu selection? 如何从AngularJS的下拉框中设置选择默认值? - How can I set a select default value from a drop-down box in AngularJS? 如何隐藏默认值 <select><option>点击下拉菜单时? - How can I hide default <select> <option> when the drop-down is clicked? 如何使div进入Bootstrap下拉菜单 - How do I make a div to a Bootstrap drop-down
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM