英文:
Showing dropdown menu using class
问题
以下是您要求的翻译部分:
我有一个菜单,我正在尝试为这个菜单创建第三级。我需要为用户悬停在其父级上的子菜单添加一个类“show-submenu”,如果用户悬停在另一个菜单项上或单击菜单外部,则删除该类。
为此,我使用了jQuery。
例如,如果用户悬停在具有“Refund policy”的li上,我需要将“show-submenu”类添加到“Sample page”的“dropdown-menu”。
如果用户悬停在“Test page”的li上,我需要将“show-submenu”类添加到“Contact”的“dropdown-menu”。
html
<li class="menu-item menu-item-has-children nav-item">
<a href="#" data-toggle="dropdown" class="dropdown-toggle nav-link show">Pages</a>
<ul class="dropdown-menu show">
<li class="menu-item nav-item">
<a href="http://localhost/login-page/" class="dropdown-item">
Login page
</a>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item">
<a href="http://localhost/refund_returns/" class="dropdown-item">
Refund policy
</a>
<ul class="dropdown-menu">
<li itemscope="itemscope" class="menu-item nav-item">
<a href="http://localhost/sample-page/" class="dropdown-item">
Sample page
</a>
</li>
</ul>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item">
<a href="http://localhost/home-logos/" class="dropdown-item">
Test page
</a>
<ul class="dropdown-menu">
<li class="menu-item nav-item">
<a href="http://localhost/kontakt/" class="dropdown-item">
Contact
</a>
</li>
</ul>
</li>
</ul>
</li>
jQuery
jQuery(document).ready(function($) {
var timeout;
$('.dropdown-item').mouseleave(function() {
clearTimeout(timeout);
$(this).closest('.dropdown-menu').find('.dropdown-menu').first().addClass('show-submenu');
});
$('.dropdown-item').mouseenter(function() {
$(this).closest('.dropdown-menu').removeClass('show-submenu');
});
$(document).on('click', function(event) {
var target = $(event.target);
// 检查单击的元素是否在菜单外部
if (!target.closest('.dropdown-menu').length) {
$('.dropdown-menu').removeClass('show-submenu');
}
});
});
此代码存在多个错误:
- "show-submenu"添加到了“dropdown-menu show”(
<ul class="dropdown-menu show">
),但我只需要添加到当前li所属的一个。 - 我只需要一个“show-submenu”,如果用户悬停在另一个菜单项上,应将其删除。
英文:
I have a menu and I am trying to make level 3 for this menu. I need to add a class "show-submenu" for the submenu whose parent the user hovered over and remove that class if the user hovered over another menu item or clicked outside the menu.
For this I use Jquery.
For example, if user hovers li with the Refund policy I need to add the "show-submenu" class to "dropdown-menu" with the Sample page
if user hovers li with the Test page I need to add "show-submenu" class to "dropdown-menu" with the Contact
html
<li class="menu-item menu-item-has-children nav-item">
<a href="#" data-toggle="dropdown"class="dropdown-toggle nav-link show" >Pages</a>
<ul class="dropdown-menu show">
<li class="menu-item nav-item">
<a href="http://localhost/login-page/" class="dropdown-item">
Login page
</a>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item">
<a href="http://localhost/refund_returns/" class="dropdown-item">
Refund policy
</a>
<ul class="dropdown-menu">
<li itemscope="itemscope" class="menu-item nav-item">
<a href="http://localhost/sample-page/" class="dropdown-item">
Sample page
</a>
</li>
</ul>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item">
<a href="http://localhost/home-logos/" class="dropdown-item">
Test page
</a>
<ul class="dropdown-menu">
<li class="menu-item nav-item">
<a href="http://localhost/kontakt/" class="dropdown-item">
Contact
</a>
</li>
</ul>
</li>
</ul>
</li>
Jquery
jQuery(document).ready(function($) {
var timeout;
$('.dropdown-item').mouseleave(function() {
clearTimeout(timeout);
$(this).closest('.dropdown-menu').find('.dropdown-menu').first().addClass('show-submenu');
});
$('.dropdown-item').mouseenter(function() {
$(this).closest('.dropdown-menu').removeClass('show-submenu');
});
$(document).on('click', function(event) {
var target = $(event.target);
// Check if the clicked element is outside the menu
if (!target.closest('.dropdown-menu').length) {
$('.dropdown-menu').removeClass('show-submenu');
}
});
});
this code has several bugs.
- show-submenu added to "dropdown-menu show" ( <ul class="dropdown-menu show"> ) but I need only one that belongs to current li
- I need to have only 1 show-submenu and remove it if user hover another menu item
答案1
得分: 1
我测试了你的 jQuery
代码,对我有效:
- 当离开
dropdown-item
时,它会向最接近的父级dropdown-menu
添加show-submenu
类。 - 当聚焦到
dropdown-item
时,它会在所有dropdown-menu
上移除所有show-submenu
类。 - 当点击菜单以外的地方时,它会在所有
dropdown-menu
上移除所有show-submenu
类。
英文:
I tested your jQuery
code and this is working for me :
- It adds a
show-submenu
class to closest parentdropdown-menu
when leaving focus ondropdown-item
- It removes all
show-submenu
classes on alldropdown-menu
when entering focus on adropdown-item
- It removes all
show-submenu
classes on alldropdown-menu
when clicking away from the menu
jQuery(document).ready(function($) {
var timeout;
$('.dropdown-item').mouseleave(function() {
$(this).closest('.dropdown-menu').addClass('show-submenu');
});
$('.dropdown-item').mouseenter(function() {
clearTimeout(timeout);
$('.dropdown-menu').removeClass('show-submenu');
});
$(document).on('click', function(event) {
var target = $(event.target);
// Check if the clicked element is outside the menu
if (!target.closest('.dropdown-menu').length) {
$('.dropdown-menu').removeClass('show-submenu');
}
});
});
> closest()
already get the first element that matches the selector by
> testing the element itself and traversing up through its ancestors in
> the DOM tree
答案2
得分: 1
你需要在li
元素上添加mouseenter
和mouseleave
事件,而不是在dropdown-item
上,这样即使鼠标移到子元素上,菜单仍然会保持可见。
下面的代码会在鼠标悬停在父元素上时显示子菜单,并在离开父元素时隐藏子菜单。
jQuery(document).ready(function($) {
// 添加鼠标事件处理程序以显示或隐藏
$('.menu-item').mouseenter(function() {
$(this).find('.dropdown-menu').addClass('show-submenu');
}).mouseleave(function() {
$(this).find('.dropdown-menu').removeClass('show-submenu');
});
$(document).on('click', function(event) {
var target = $(event.target);
// 检查点击的元素是否在菜单外部
if (!target.closest('.dropdown-menu').length) {
$('.dropdown-menu').removeClass('show-submenu');
}
});
});
.dropdown-menu {
display: none;
}
.show, .show-submenu {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="menu-item menu-item-has-children nav-item">
<a href="#" data-toggle="dropdown" class="dropdown-toggle nav-link show">Pages</a>
<ul class="dropdown-menu show">
<li class="menu-item nav-item">
<a href="http://localhost/login-page/" class="dropdown-item">
Login page
</a>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item">
<a href="http://localhost/refund_returns/" class="dropdown-item">
Refund policy
</a>
<ul class="dropdown-menu">
<li itemscope="itemscope" class="menu-item nav-item">
<a href="http://localhost/sample-page/" class="dropdown-item">
Sample page
</a>
</li>
</ul>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item">
<a href="http://localhost/home-logos/" class="dropdown-item">
Test page
</a>
<ul class="dropdown-menu">
<li class="menu-item nav-item">
<a href="http://localhost/kontakt/" class="dropdown-item">
Contact
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
英文:
You need to add mouseenter and mouseleave on li element instead of dropdown-item so that it would stay visible even if you hover mouse to the child elements.
The below code will show the submenu when you hover the mouse and hide the same submenu when you leave the mouse from parent.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
jQuery(document).ready(function($) {
// add mouse event handler to show or hide
$('.dropdown-item').each(function() {
$(this).closest('.menu-item').mouseenter(function() {
$(this).find('.dropdown-menu').addClass('show-submenu');
}).mouseleave(function() {
$(this).find('.dropdown-menu').removeClass('show-submenu');
});
});
$(document).on('click', function(event) {
var target = $(event.target);
// Check if the clicked element is outside the menu
if (!target.closest('.dropdown-menu').length) {
$('.dropdown-menu').removeClass('show-submenu');
}
});
});
<!-- language: lang-css -->
.dropdown-menu {
display: none;
}
.show, .show-submenu {
display: block;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="menu-item menu-item-has-children nav-item">
<a href="#" data-toggle="dropdown" class="dropdown-toggle nav-link show">Pages</a>
<ul class="dropdown-menu show">
<li class="menu-item nav-item">
<a href="http://localhost/login-page/" class="dropdown-item">
Login page
</a>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item">
<a href="http://localhost/refund_returns/" class="dropdown-item">
Refund policy
</a>
<ul class="dropdown-menu">
<li itemscope="itemscope" class="menu-item nav-item">
<a href="http://localhost/sample-page/" class="dropdown-item">
Sample page
</a>
</li>
</ul>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item">
<a href="http://localhost/home-logos/" class="dropdown-item">
Test page
</a>
<ul class="dropdown-menu">
<li class="menu-item nav-item">
<a href="http://localhost/kontakt/" class="dropdown-item">
Contact
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论