用Jquery显示/隐藏两个Div

时间:2013-11-14 21:36:25

标签: javascript jquery jquery-selectors

我有两个div:

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

        </div>

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

        </div>

Jquery代码:

<script type="text/javascript">

    $(document).ready(function(){

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

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

  });

  </script>

我每次点击“组件展开”时都会尝试打开一个DIV。现在打开两个 我很高兴他们帮助我的人

2 个答案:

答案 0 :(得分:1)

您需要使您的选择器具体,请使用next

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

尝试:

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

<强> Demo

答案 1 :(得分:1)

使用this.next

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