简体   繁体   中英

DropDown multilevel menu with hover

The following is basic working DropDown menu, child's appear on hover the parent text.

<script type="text/javascript">
  $(document).ready(function () { 
    $('#nav ul').hide();

    $('#nav li > a').hover(
      function () {$('ul', this.parentNode).stop().slideDown(100);}
    );

   $('#nav li').hover(null, 
      function (e) {$('ul', this.parentNode).stop().slideUp(100);}
   );

  }
  );
</script>

<ul id="nav">
  <li><a href="#">Parent A</a>
    <ul>
      <li><a href="#">Sub a1</a>
          <ul>
            <li><a href="#">Item a1.1</a></li>
            <li><a href="#">Item a1.2</a></li>
            <li><a href="#">Item a1.3</a></li>
        </ul> 
      </li>
      <li><a href="#">Sub a2</a></li>
      <li><a href="#">Sub a3</a></li>
    </ul>
  </li>
  <li><a href="#">Parent B</a>
    <ul>
      <li><a href="#">Sub b1</a></li>
      <li><a href="#">Sub b2</a></li>
      <li><a href="#">Sub b3</a></li>
    </ul> 
  </li>
</ul>

I've added above a third level under submenu, but it's not opening and closing the last level.

I'm not sure in this case if I need to change the html code or is it only the JavaScript that needs to be changed so that the script will work properly.

You can do it as follows:

   $('#nav ul').hide();

   $('#nav li > a').hover(
      function () {
      //show its submenu
        $(this).parent().children('ul').stop().slideDown(100);
      }
    );
   $('#nav li').hover(null, 
      function (e) {
      //hide its submenu
        $(this).children('ul').stop().slideUp(100);
      }
   );

See working demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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