简体   繁体   中英

Show/Hide two Divs with Jquery

i have two div's:

         <a href="#" class="component_expand"></a>
        <div class="component_wrapper">

        </div>

         <a href="#" class="component_expand"></a>
        <div class="component_wrapper">

        </div>

Jquery code:

<script type="text/javascript">

    $(document).ready(function(){

    $(".component_wrapper").hide();
    $(".component_expand").show();

    $('.component_expand').click(function(){
    $(".component_wrapper").slideToggle();
    });

  });

  </script>

I try every time I click on "component expand" one DIV open. Now open two I'd be happy with who they help me on

You need to make your selector specific, use next .

$(this).next().slideToggle();

Try:

 $(document).ready(function(){
    $(".component_wrapper").hide(); //You can do this using css rule itself
    $(".component_expand").show().click(function(){
        $(this).next().slideToggle();
    });
  });

Demo

Use an instance of this and .next

$('.component_expand').click(function(){
    $(this).next(".component_wrapper").slideToggle();
});

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