最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery ul li toggle menu - Stack Overflow

matteradmin4PV0评论

I have my HTML code here :

<ul class="main-block">

 <li class="firstLevel">
   <a href="#category">EXAMPLE CATEGORY 1</a>
     <ul class="dijete">
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 11.</a>
       </li>
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 1.1</a>
       </li>
     </ul>
 </li>

 <li class="firstLevel">
   <a href="#category">EXAMPLE CATEGORY 2</a>
     <ul class="dijete">
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.1</a>
       </li>
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.2</a>
       </li>
     </ul>
 </li>

</ul>

My jQuery code for toggle() is here:

<script type="text/javascript">
    var $j = jQuery.noConflict();
    $j(function() {
        $j('li.firstLevel').click(function(){
            if($j('ul.dijete').hasClass('active')){
                $j(this).find('ul.dijete').removeClass('active');
            }else{
                $j(this).find('ul.dijete').addClass('active');
            }
        });
    });
</script>

But when I am on one category EXAMPLE 1 (active or clicked already) and chose to click on another EXAMPLE 2 - the first one does not close, so as the other second that I clicked doesn not open.

Why is that hide-show doesn't work?

Why I can not show second sub-menu and hide first one while I am currently on first (not working in both ways)?

I have my HTML code here :

<ul class="main-block">

 <li class="firstLevel">
   <a href="#category">EXAMPLE CATEGORY 1</a>
     <ul class="dijete">
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 11.</a>
       </li>
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 1.1</a>
       </li>
     </ul>
 </li>

 <li class="firstLevel">
   <a href="#category">EXAMPLE CATEGORY 2</a>
     <ul class="dijete">
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.1</a>
       </li>
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.2</a>
       </li>
     </ul>
 </li>

</ul>

My jQuery code for toggle() is here:

<script type="text/javascript">
    var $j = jQuery.noConflict();
    $j(function() {
        $j('li.firstLevel').click(function(){
            if($j('ul.dijete').hasClass('active')){
                $j(this).find('ul.dijete').removeClass('active');
            }else{
                $j(this).find('ul.dijete').addClass('active');
            }
        });
    });
</script>

But when I am on one category EXAMPLE 1 (active or clicked already) and chose to click on another EXAMPLE 2 - the first one does not close, so as the other second that I clicked doesn not open.

Why is that hide-show doesn't work?

Why I can not show second sub-menu and hide first one while I am currently on first (not working in both ways)?

Share Improve this question edited Mar 8, 2016 at 23:16 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Mar 8, 2016 at 22:57 Ho Thanh Cyberdelia NhanHo Thanh Cyberdelia Nhan 4465 silver badges21 bronze badges 3
  • Actually I am adding a CSS class, instad using jQuery toggle();? Class has CSS style display:block (default none) OR should I use hide() show() for displaying child ul content? – Ho Thanh Cyberdelia Nhan Commented Mar 8, 2016 at 23:00
  • 1 try using jquery's toggle function. – Keith Anderson Commented Mar 8, 2016 at 23:06
  • Possible duplicate of jQuery toggleClass if else not working – Brian Tompsett - 汤莱恩 Commented May 8, 2016 at 8:52
Add a ment  | 

2 Answers 2

Reset to default 3

Use this:

var $j = jQuery.noConflict();
$j(function() {
    $j('li.firstLevel').click(function(){
        $j(this).children('ul.dijete').toggleClass('active');
    });
});

Working fiddle

You could use toggleClass() function instead and before adding active class to the clicked item you should remove it from other items with class dijete using :

$('ul.dijete').removeClass('active');

Hope this helps.


Working snippet

$('li.firstLevel').click(function(e){
  e.preventDefault();

  $('ul.dijete').removeClass('active');
  $(this).find('ul.dijete').toggleClass('active');
});
.active{
  background-color: green;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="main-block">

  <li class="firstLevel">
    <a href="#category">EXAMPLE CATEGORY 1</a>
    <ul class="dijete">
      <li class="child">
        <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 11.</a>
      </li>
      <li class="child">
        <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 1.1</a>
      </li>
    </ul>
  </li>

  <li class="firstLevel">
    <a href="#category">EXAMPLE CATEGORY 2</a>
    <ul class="dijete">
      <li class="child">
        <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.1</a>
      </li>
      <li class="child">
        <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.2</a>
      </li>
    </ul>
  </li>

</ul>

Post a comment

comment list (0)

  1. No comments so far