简体   繁体   English

用Jquery显示/隐藏两个Div

[英]Show/Hide two Divs with Jquery

i have two div's: 我有两个div:

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

        </div>

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

        </div>

Jquery code: Jquery代码:

<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. 我每次点击“组件展开”时都会尝试打开一个DIV。 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 使用的一个实例, this.next

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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