How make li elements slide down and up?

前端 未结 5 1542
孤街浪徒
孤街浪徒 2021-01-26 07:22

I have

  • elements in a
      and I want them expand down and up and each element should expand down and up.

      HTML:

      
      
              
  • 5条回答
    •  深忆病人
      2021-01-26 08:06

      Target the clicked elements parent instead of all the .answers

      $(document).ready(function(){
         $('.expand').click(function(){
            $(this).parent().css('height','auto');  
         })
      });
      

      http://jsfiddle.net/Tmm6R/ fiddle

      To toggle the others, target the siblings and set them to default

      $(document).ready(function(){
         $('.expand').click(function(){
              $(this).parent().css('height','auto'); 
              $(this).parent().siblings().css("height", "20px");
         })
      });
      

      http://jsfiddle.net/Tmm6R/1/

      If you also want to the opened one to be togglabble, you could do something like this:

      $(document).ready(function(){
          $('.expand').click(function(){        
              var parent = $(this).parent();
              console.log(parent.css("height"));
              if(parent.css("height") > "20px"){
                 parent.css("height", "20px");   
              } else {
                 parent.css("height", "auto");   
              }
      
              $(this).parent().siblings().css("height", "20px");
          })
      });
      

      http://jsfiddle.net/TCxP5/

    提交回复
    热议问题