简体   繁体   English

单击外部菜单时如何删除切换的类

[英]How to remove toggled class when clicking outside menu

The dropdown menu is designed with CSS and HTML/JS using a class called "is-open" that is added from JS.下拉菜单使用 CSS 和 HTML/JS 设计,使用从 JS 添加的名为“is-open”的类。 Once present inside the HTML div specified, it will activate the CSS to display the submenu.一旦出现在指定的 HTML div 中,它将激活 CSS 以显示子菜单。

However there is a small issue wherein the dropdown menu once clicked will not disappear unless the same menu item is clicked.但是有一个小问题,即一旦单击下拉菜单就不会消失,除非单击相同的菜单项。 (The class will not de-toggle when clicking outside the menu-content div) (在菜单内容 div 外部单击时,该类不会取消切换)

As a basic functionality this menu needs to disappear once a user clicks not just on the menu, but anywhere on the page.作为一项基本功能,一旦用户不仅单击菜单,而且单击页面上的任何位置,该菜单都需要消失。

My present javascript is the following:我现在的 javascript 如下:

$(document).ready(function() {
   $(".has-submenu").click(function(e) {
      e.stopPropagation();
      if($(this).hasClass("is-open")) {
         $(this).removeClass("is-open");
      } else {
         $(".has-submenu").removeClass("is-open");
         $(this).addClass("is-open");
      }
   });
});

Here is a codepen example of the code: https://codepen.io/anon/pen/EwMjrz这是代码的codepen示例: https ://codepen.io/anon/pen/EwMjrz

You could add an event listener to the document to close your menu like so您可以向文档添加事件侦听器以关闭菜单,如下所示

$(document).ready(function() {
  $(".has-submenu").click(function(e) {
    e.stopPropagation();
    if($(this).hasClass("is-open")) {
      $(this).removeClass("is-open");
    } else {
      $(".has-submenu").removeClass("is-open");
      $(this).addClass("is-open");
    }
  });
  $(document).on('click', function (e) {
    e.stopPropagation();
    $('.has-submenu').removeClass("is-open");
  });
});

This should do the trick!这应该可以解决问题!

I want to add an additional answer that is simpler to implement and prevents any unwanted potential blocking behaviour by adding the click to the document.我想添加一个更易于实现的附加答案,并通过将单击添加到文档来防止任何不需要的潜在阻止行为。

  1. Add tabindex="-1" to the div which opens to show the menu when the is-open class is added将 tabindex="-1" 添加到打开的 div 以在添加 is-open 类时显示菜单
  2. When applying the is-open class to open and display the menu apply focus() to the element.当应用 is-open 类打开和显示菜单时,将 focus() 应用于元素。 This will allow for you to attach a blur event listener这将允许您附加一个模糊事件侦听器
  3. create the onblur event listener and simply remove the is-open class on blur and your menu will close whenever the click anywhere outside of the element.创建 onblur 事件侦听器并简单地删除模糊上的 is-open 类,只要单击元素外的任何位置,您的菜单就会关闭。

This can prevent dom from intercepting click events why avoiding attaching the onclick event to the document or body as some other answers suggest.这可以防止 dom 拦截单击事件,为什么要避免将 onclick 事件附加到文档或正文,正如其他一些答案所建议的那样。 This answer here explains a bit more about the tabindex aspect which is how i figured out to use the focus and blur events: https://stackoverflow.com/a/46115264/12212051这个答案在这里解释了关于 tabindex 方面的更多信息,这是我想出使用焦点和模糊事件的方式: https ://stackoverflow.com/a/46115264/12212051

<div class="sel" id="monthSelectDiv" aria-label="select month" tabindex="-1">
    <select id="monthSelect" name="monthSelect">
        <option disabled>Select Month</option>
        <option value="01"> January </option>                            
        <option value="02"> February </option>
    </select>
</div>

<script>
// Toggling the `.active` state on the `.sel`.
$('.sel').click(function () {
    $(this).toggleClass('active');
    $(this).focus();
});

$('.sel').blur(function () {
    $(this).removeClass('active');
});
</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM