[英]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> 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.