Hide and Show list menu items using jQuery

2019-09-08 14:04发布

问题:

I have simple vertical menu using list elements like below

<ul id="leftNav">
    <li id="home"><a href="/index.html">Home</a>
    </li>
    <li id="apples"><a href="/category/apples.html">Apples</a>

        <ul class="subMenu">
            <li><a href="/category/red-apples.html">Red Apples</a>
            </li>
            <li><a href="/category/green-apples.html">Green Apples</a> 
            </li>
            <li><a href="/category/golden-apples.html">Golden Apples</a>
            </li>
        </ul>
    </li>
    <li id="grapes"><a href="/category/grapes.html">Grapes</a>

        <ul class="subMenu">
            <li><a href="/category/red-grapes.html">Red Grapes</a>
            </li>
            <li><a href="/category/green-grapes.html">Green Grapes</a>
            </li>
            <li><a href="/category/black-grapes.html">Black Grapes</a>
            </li>
        </ul>
    </li>
    <li id="dry-fruits"><a href="/category/dry-fruits.html">Dry Fruits</a>

        <ul class="subMenu">
            <li id="subParent1"><a href="#">Fruits That Are Dried</a>

                <ul class="subMenu1">
                    <li><a href="/category/figs.html">Figs</a>
                    </li>
                    <li><a href="/category/dates.html">Dates</a>
                    </li>
                    <li><a href="/category/pineapples.html">Pine Apples</a>
                    </li>
                </ul>
            </li>
            <li id="subParent2"><a href="#">Nuts and Seeds</a>

                <ul class="subMenu1">
                    <li><a href="/category/chestnuts.html">Chestnuts</a>
                    </li>
                    <li><a href="/category/almonds.html">Almonds</a>
                    </li>
                    <li><a href="/category/walnuts.html">Walnuts</a>
                    </li>
                </ul>
            </li>
            <li id="subParent3"><a href="/category/bananas.html">Bananas</a>
            </li>
        </ul>
    </li>
    <li id="sale" class="expanded"><a href="/category/sale.html">Sale</a>

</ul>

What I am trying to do is when apples or its sub items are clicked I am trying keep that sections of the list expanded and so on, so when Grapes or its sub items are clicked all other should be closed except Grapes section.

I tried using code like below, but since Apples and Grapes are links which render their respective pages, my code below is not working.

$(document).ready(function() {
    $("#apples .subMenu").css("display", "block");
}); 

Any help or example or advice is appreciated.

回答1:

You constantly reload page - and the javascript reloads too. If you want to save the state of the menu between requests use cookies.

Or here is a version without reloading the page - then you have to use Ajax.

<script type="text/javascript">
$(document).ready(function() {

    $('#left-navigation a').click(function(){
     return false;
    });

    $(".parent-grapes > a, .parent-apples > a, .parent-dry-fruits > a").click(function () {
    var $block = $(this).parent().find(".sub-menu");
    $block.toggle();

    $.get($(this).attr('href'), function(data){
      $('#main-content').html($(data).find('#main-content').html());
    });
    return false;
    });
});
</script>

The menu requires only this javascript (and JQuery)



回答2:

  <script type="text/javascript">
      $(document).ready(function() {
         $("#apples > a").on('click',function(e){
            $('#apples .subMenu ').toggle();
            return false;
         });
      }); 
   </script>

http://jsfiddle.net/makedone/WRcBa/



回答3:

try this. It worked for me..

Thanks! @leo.

 <script type="text/javascript">
      $(document).ready(function() {
            hideAll();

             $("#leftNav li a").click( function(e) {
               try {

                  var pid = $(this).parent('li').attr("id");
                  //alert(pid);
                  if(pid == undefined) {

                  } else {
                    hideAll();
                    $("#" + pid + " .submenu").show();
                    e.preventDefault();
                  }

               } catch(e) {
                alert("oops!");
               }
            });
        }); 


        function hideAll() { 
             $(".subMenu").hide();
        }

  </script>