【问题标题】:Two pull down menus by clicking通过单击两个下拉菜单
【发布时间】:2018-08-28 10:16:39
【问题描述】:

点击第4点时如何实现第1项菜单下的隐藏?

我通过添加检查来实现:

if ($('.arm-new-ext').children('ul').hasClass('show')) $('.arm-new-ext').click();

和

if ($('.arm-ext').children('ul').hasClass('show')) $('.arm-ext').click();

这是一个正确的决定吗?你将如何实现它?

.main-menu {
  
  
}

.main-menu li {
  list-style: outside none none;
  cursor: pointer;
  float: left;
}

.hide {
  display: none;
}

.show {
  display: block;
}

ul.menu {
  border: 1px solid #95958d;
  position: absolute;
  margin: 0;
  padding: 0;
}

ul.menu li {
  font: 14px Segoe UI, sans-serif;
  list-style: outside none none;
  position: static;
  line-height: 20px;
  text-align: left;
  clear: both;
  background: #faf8f5;
  padding: 2px 0px;
  white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
  $(function() {
    $('.arm-new-ext').click(function(e) {
      // My solution!
     // if ($('.arm-ext').children('ul').hasClass('show')) $('.arm-ext').click();

      if ($(this).children('ul').hasClass('show'))
        $(this).children('ul')
        .removeClass('show')
        .addClass('hide');
      else
        $(this).children('ul')
        .addClass('show')
        .removeClass('hide');

      if (e.stopPropagation) e.stopPropagation();
      if (e.preventDefault) e.preventDefault();
    });

    $('.arm-ext').click(function(e) {
      // My solution!
     // if ($('.arm-new-ext').children('ul').hasClass('show')) $('.arm-new-ext').click();

      if ($(this).children('ul').hasClass('show'))
        $(this).children('ul')
        .removeClass('show')
        .addClass('hide');
      else
        $(this).children('ul')
        .addClass('show')
        .removeClass('hide');

      if (e.stopPropagation) e.stopPropagation();
      if (e.preventDefault) e.preventDefault();
    });

    $('body').click(function() {
      if ($('.arm-new-ext').children('ul').hasClass('show'))
        $('.arm-new-ext').children('ul')
        .removeClass('show')
        .addClass('hide');

      if ($('.arm-ext').children('ul').hasClass('show'))
        $('.arm-ext').children('ul')
        .removeClass('show')
        .addClass('hide');
    })
  });
</script>
<body style="border:1px solid red">
<ul class="main-menu">
  <li class="arm-ext">
    <span>Пункт 1</span>
    <ul class="menu hide">
      <li>1</li>
      <li>2</li>
    </ul>
  </li>
  <li>Пункт 2</li>
  <li>Пункт 3</li>
  <li class="arm-new-ext">
    <span>Пункт 4</span>
    <ul class="menu hide">
      <li>1</li>
      <li>2</li>
    </ul>
  </li>
  <li>Пункт 5</li>
</ul>
</body>

【问题讨论】:

    标签: jquery html css drop-down-menu click


    【解决方案1】:

    您可以使用toggleClass 来实现此目的:

    var $menuHeader = $('.arm-new-ext,.arm-ext');
    var $menuItems = $menuHeader.children('ul');
    $menuHeader.click(function(e) {
        var $currentMenuItems = $(this).children('ul');
        $menuItems.not($currentMenuItems).removeClass('show').addClass('hide')
        $currentMenuItems.toggleClass('show hide');
    });
    

    .main-menu {
      
      
    }
    
    .main-menu li {
      list-style: outside none none;
      cursor: pointer;
      float: left;
    }
    
    .hide {
      display: none;
    }
    
    .show {
      display: block;
    }
    
    ul.menu {
      border: 1px solid #95958d;
      position: absolute;
      margin: 0;
      padding: 0;
    }
    
    ul.menu li {
      font: 14px Segoe UI, sans-serif;
      list-style: outside none none;
      position: static;
      line-height: 20px;
      text-align: left;
      clear: both;
      background: #faf8f5;
      padding: 2px 0px;
      white-space: nowrap;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script>
      $(function() {
        
    var $menuHeader = $('.arm-new-ext,.arm-ext');
    var $menuItems = $menuHeader.children('ul');
    $menuHeader.click(function(e) {
       debugger
        var $currentMenuItems = $(this).children('ul');
        $menuItems.not($currentMenuItems).removeClass('show').addClass('hide')
        $currentMenuItems.toggleClass('show hide');
       
    
          if (e.stopPropagation) e.stopPropagation();
          if (e.preventDefault) e.preventDefault();
        });
    
        
    
        $('body').click(function() {
          if ($('.arm-new-ext').children('ul').hasClass('show'))
        $('.arm-new-ext').children('ul')
        .removeClass('show')
        .addClass('hide');
    
      if ($('.arm-ext').children('ul').hasClass('show'))
        $('.arm-ext').children('ul')
        .removeClass('show')
        .addClass('hide');
        })
      });
    </script>
    <body style="border:1px solid red">
    <ul class="main-menu">
      <li class="arm-ext">
        <span>Пункт 1</span>
        <ul class="menu hide">
          <li>1</li>
          <li>2</li>
        </ul>
      </li>
      <li>Пункт 2</li>
      <li>Пункт 3</li>
      <li class="arm-new-ext">
        <span>Пункт 4</span>
        <ul class="menu hide">
          <li>1</li>
          <li>2</li>
        </ul>
      </li>
      <li>Пункт 5</li>
    </ul>
    </body>

    【讨论】:

    • 不起作用。打开第二个子菜单时,第一个子菜单不关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2015-12-29
    • 2021-09-06
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多