Showing content based on a nav link clicked

后端 未结 4 1355
长情又很酷
长情又很酷 2021-01-16 13:09

I have a menu selection that looks like this:

  • About
4条回答
  •  失恋的感觉
    2021-01-16 13:36

    You have a couple of problems with your JS:

    • you select items by class .misc instead of ID #misc
    • the way you coded the click is very rigid. Make it more elastic like:

      $('.menu').on('click', 'li', function (e) {
          $('article').hide();
          var id = $(this).find('a').attr('href'); // $(this) is the clicked LI
          $(id).show();    
      })
      

    I assume all items with IDs #about, #contact etc. are simply article HTML elements and I hide them all before showing the right one. Hope this helps.

    EDIT

    Or even a bit more elegant hiding the other contents:

    $('.menu').on('click', 'li', function (e) {
        var id = $(this).find('a').attr('href');
        $(id).show().siblings().hide();    
    })
    

提交回复
热议问题