H
H
hypepc2018-03-22 22:53:25
JavaScript
hypepc, 2018-03-22 22:53:25

Hide block when clicking on another block?

Hello.

The essence of the question is this.

There is a menu. Clicking on a menu item opens a submenu. Clicking on the second menu item opens another submenu. You need to make sure that when you click on the next item, the previous submenu is hidden and a new one is opened.

Help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TANK_IST, 2018-03-29
@TANK_IST

Let's say the class is responsible for the open menu show
. Then everything is simple:

$('nav li').click(function() {
        $(this).parents('nav').find('.show').removeClass('show');
        $(this).addClass('show').parents('li').addClass('show');
    });

C
cccr85, 2018-03-29
@cccr85

Like this, didn't check

<ul class="menu">
   <li>
       <a href="#" title="">Ссылка</a>
       <ul>
       <li>
            <a href="#" title="">Ссылка</a>
            <ul>
           <li><a href="#" title="">Ссылка</a></li>
           <li><a href="#" title="">Ссылка</a></li>
        </ul>
       </li>
       <li>
            <a href="#" title="">Ссылка</a>
            <ul>
           <li><a href="#" title="">Ссылка</a></li>
           <li><a href="#" title="">Ссылка</a></li>
        </ul>
        </li>
    </ul>
   </li>
   <li>
      <a href="#" title="">Ссылка</a>
      <ul>
       <li>
            <a href="#" title="">Ссылка</a>
            <ul>
           <li><a href="#" title="">Ссылка</a></li>
           <li><a href="#" title="">Ссылка</a></li>
        </ul>
       </li>
       <li>
            <a href="#" title="">Ссылка</a>
            <ul>
           <li><a href="#" title="">Ссылка</a></li>
           <li><a href="#" title="">Ссылка</a></li>
        </ul>
        </li>
    </ul>
    </li>
</ul>

$(function(){
  $('.menu').find('a').on('click.menu', function(e){
    e.preventDefault();
    var $parent = $(this).parent();
    if ($parent.hasClass('active')) {
      $parent.removeClass('active');
    } else {
      $parent.parent().find('.active').removeClass('active');
      $parent.addClass('active');
    }
  });
});

.menu ul { display: none; }
.menu li.active > ul { display: block; }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question