gpt4 book ai didi

javascript - 如何将两级 jQuery 扩展菜单更改为三级?

转载 作者:太空宇宙 更新时间:2023-11-04 15:43:01 25 4
gpt4 key购买 nike

我目前有一个使用 jQuery 触发的 2 级扩展菜单。我一直在兜圈子,一直对如何更新 if/else 语句以使其适用于多级(第三层)感到困惑。我想知道是否有人可以帮我完成这件事?

jsFiddle example

提前致谢!

HTML:

<ul id="MobileNav" class="mobile-nav">
<li class="mobile-nav__item">
<button type="button" class="js-toggle-submenu mobile-nav__link" data-level="1" aria-expanded="false">
<span class="mobile-nav__label">Link Level 1 - 1</span>
</button>
<ul class="mobile-nav__dropdown" data-level="2">
<li class="mobile-nav__item">
<button type="button" class="js-toggle-submenu mobile-nav__link" aria-expanded="false">
<span class="mobile-nav__label">Link Level 2 - 1</span>
</button>
<ul class="mobile-nav__dropdown" data-level="3">
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 3 - 1</span>
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="mobile-nav__item">
<button type="button" class="js-toggle-submenu mobile-nav__link" data-level="1" aria-expanded="false">
<span class="mobile-nav__label">Link Level 1 - 2</span>
</button>
<ul class="mobile-nav__dropdown" data-level="2">
<li class="mobile-nav__item">
<button type="button" class="js-toggle-submenu mobile-nav__link" aria-expanded="false">
<span class="mobile-nav__label">Link Level 2 - 1</span>
</button>
<ul class="mobile-nav__dropdown" data-level="3">
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 3 - 1</span>
</a>
</li>
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 3 - 2</span>
</a>
</li>
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 3 - 3</span>
</a>
</li>
</ul>
</li>
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 2 - 2</span>
</a>
</li>
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 2 - 3</span>
</a>
</li>
</ul>
</li>
</ul>

JS:

var accordion_head  = $('.js-toggle-submenu'),
accordion_body = $('.mobile-nav__dropdown');

accordion_head.on('click', function(event) {
event.preventDefault();

if ($(this).hasClass('active')) {
accordion_body.slideUp('normal');
accordion_head.removeClass('active');
} else {
accordion_body.slideUp('normal');
$(this).next().stop(true,true).slideToggle('normal');
accordion_head.removeClass('active');
$(this).addClass('active');
}
});

最佳答案

您可以使您的下拉列表适用于任何嵌套级别。您正在使用条件扩展菜单以检查不需要的 active 类。仅用一行 code 切换 active 类和 slideUpslideDown

这是演示

jQuery(document).ready(function($) {
var accordion_head = $('.js-toggle-submenu');

accordion_head.on('click', function(event) {
event.preventDefault();
if($(this).hasClass('active')){
$(this).next('.mobile-nav__dropdown').find('.active').removeClass('active').next('.mobile-nav__dropdown').slideUp('normal');
}
$(this).parent('li').siblings('li').find('.active').removeClass('active').next('.mobile-nav__dropdown').slideUp('normal');
$(this).stop().toggleClass('active').next('.mobile-nav__dropdown').stop().slideToggle('normal');
});
});
.mobile-nav__dropdown {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="MobileNav" class="mobile-nav">
<li class="mobile-nav__item">
<button type="button" class="js-toggle-submenu mobile-nav__link" data-level="1" aria-expanded="false">
<span class="mobile-nav__label">Link Level 1 - 1</span>
</button>
<ul class="mobile-nav__dropdown" data-level="2">
<li class="mobile-nav__item">
<button type="button" class="js-toggle-submenu mobile-nav__link" aria-expanded="false">
<span class="mobile-nav__label">Link Level 2 - 1</span>
</button>
<ul class="mobile-nav__dropdown" data-level="3">
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 3 - 1</span>
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="mobile-nav__item">
<button type="button" class="js-toggle-submenu mobile-nav__link" data-level="1" aria-expanded="false">
<span class="mobile-nav__label">Link Level 1 - 2</span>
</button>
<ul class="mobile-nav__dropdown" data-level="2">
<li class="mobile-nav__item">
<button type="button" class="js-toggle-submenu mobile-nav__link" aria-expanded="false">
<span class="mobile-nav__label">Link Level 2 - 1</span>
</button>
<ul class="mobile-nav__dropdown" data-level="3">
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 3 - 1</span>
</a>
</li>
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 3 - 2</span>
</a>
</li>
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 3 - 3</span>
</a>
</li>
</ul>
</li>
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 2 - 2</span>
</a>
</li>
<li class="mobile-nav__item">
<a href="/" class="mobile-nav__sublist-link">
<span class="mobile-nav__label">Link Level 2 - 3</span>
</a>
</li>
</ul>
</li>
</ul>

关于javascript - 如何将两级 jQuery 扩展菜单更改为三级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58749241/

25 4 0