使用类显示下拉菜单

huangapple go评论76阅读模式
英文:

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');
    }
  });
});

此代码存在多个错误:

  1. "show-submenu"添加到了“dropdown-menu show”(<ul class="dropdown-menu show">),但我只需要添加到当前li所属的一个。
  2. 我只需要一个“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

&lt;li  class=&quot;menu-item menu-item-has-children nav-item&quot;&gt;
&lt;a  href=&quot;#&quot; data-toggle=&quot;dropdown&quot;class=&quot;dropdown-toggle nav-link show&quot; &gt;Pages&lt;/a&gt;
&lt;ul class=&quot;dropdown-menu show&quot;&gt;
&lt;li class=&quot;menu-item  nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/login-page/&quot; class=&quot;dropdown-item&quot;&gt;
Login page
&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-has-children dropdown nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/refund_returns/&quot; class=&quot;dropdown-item&quot;&gt;
Refund policy
&lt;/a&gt;
&lt;ul class=&quot;dropdown-menu&quot;&gt;
&lt;li itemscope=&quot;itemscope&quot; class=&quot;menu-item nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/sample-page/&quot; class=&quot;dropdown-item&quot;&gt;
Sample page
&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-has-children dropdown nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/home-logos/&quot; class=&quot;dropdown-item&quot;&gt;
Test page
&lt;/a&gt;
&lt;ul class=&quot;dropdown-menu&quot;&gt;
&lt;li class=&quot;menu-item nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/kontakt/&quot; class=&quot;dropdown-item&quot;&gt;
Contact
&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;

Jquery

	jQuery(document).ready(function($) {
var timeout;
$(&#39;.dropdown-item&#39;).mouseleave(function() {
clearTimeout(timeout);
$(this).closest(&#39;.dropdown-menu&#39;).find(&#39;.dropdown-menu&#39;).first().addClass(&#39;show-submenu&#39;);
});
$(&#39;.dropdown-item&#39;).mouseenter(function() {
$(this).closest(&#39;.dropdown-menu&#39;).removeClass(&#39;show-submenu&#39;);
});
$(document).on(&#39;click&#39;, function(event) {
var target = $(event.target);
// Check if the clicked element is outside the menu
if (!target.closest(&#39;.dropdown-menu&#39;).length) {
$(&#39;.dropdown-menu&#39;).removeClass(&#39;show-submenu&#39;);
}
});
});

this code has several bugs.

  1. show-submenu added to "dropdown-menu show" ( <ul class="dropdown-menu show"> ) but I need only one that belongs to current li
  2. 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 parent dropdown-menu when leaving focus on dropdown-item
  • It removes all show-submenu classes on all dropdown-menu when entering focus on a dropdown-item
  • It removes all show-submenu classes on all dropdown-menu when clicking away from the menu
jQuery(document).ready(function($) {
      var timeout;
    
      $(&#39;.dropdown-item&#39;).mouseleave(function() {
        $(this).closest(&#39;.dropdown-menu&#39;).addClass(&#39;show-submenu&#39;);
      });
    
      $(&#39;.dropdown-item&#39;).mouseenter(function() {
        clearTimeout(timeout);
        $(&#39;.dropdown-menu&#39;).removeClass(&#39;show-submenu&#39;);
      });
    
      $(document).on(&#39;click&#39;, function(event) {
        var target = $(event.target);
    
        // Check if the clicked element is outside the menu
        if (!target.closest(&#39;.dropdown-menu&#39;).length) {
          $(&#39;.dropdown-menu&#39;).removeClass(&#39;show-submenu&#39;);
        }
      });
});

> 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

https://jsfiddle.net/bLsyrncz/44/

答案2

得分: 1

你需要在li元素上添加mouseentermouseleave事件,而不是在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
$(&#39;.dropdown-item&#39;).each(function() {
$(this).closest(&#39;.menu-item&#39;).mouseenter(function() {
$(this).find(&#39;.dropdown-menu&#39;).addClass(&#39;show-submenu&#39;);
}).mouseleave(function() {
$(this).find(&#39;.dropdown-menu&#39;).removeClass(&#39;show-submenu&#39;);
});
});
$(document).on(&#39;click&#39;, function(event) {
var target = $(event.target);
// Check if the clicked element is outside the menu
if (!target.closest(&#39;.dropdown-menu&#39;).length) {
$(&#39;.dropdown-menu&#39;).removeClass(&#39;show-submenu&#39;);
}
});
});

<!-- language: lang-css -->

.dropdown-menu {
display: none;
}
.show, .show-submenu {
display: block;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;ul&gt;
&lt;li class=&quot;menu-item menu-item-has-children nav-item&quot;&gt;
&lt;a href=&quot;#&quot; data-toggle=&quot;dropdown&quot; class=&quot;dropdown-toggle nav-link show&quot;&gt;Pages&lt;/a&gt;
&lt;ul class=&quot;dropdown-menu show&quot;&gt;
&lt;li class=&quot;menu-item nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/login-page/&quot; class=&quot;dropdown-item&quot;&gt;
Login page
&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-has-children dropdown nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/refund_returns/&quot; class=&quot;dropdown-item&quot;&gt;
Refund policy
&lt;/a&gt;
&lt;ul class=&quot;dropdown-menu&quot;&gt;
&lt;li itemscope=&quot;itemscope&quot; class=&quot;menu-item nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/sample-page/&quot; class=&quot;dropdown-item&quot;&gt;
Sample page
&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-has-children dropdown nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/home-logos/&quot; class=&quot;dropdown-item&quot;&gt;
Test page
&lt;/a&gt;
&lt;ul class=&quot;dropdown-menu&quot;&gt;
&lt;li class=&quot;menu-item nav-item&quot;&gt;
&lt;a href=&quot;http://localhost/kontakt/&quot; class=&quot;dropdown-item&quot;&gt;
Contact
&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 19:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定