简体   繁体   English

使用 jquery 显示/隐藏多个 div

[英]show/hide mulptiple divs using jquery

im very new to jquery , i understand some of it but this is hitting me hard.我对 jquery 很陌生,我理解其中的一些,但这对我打击很大。 so ive looked around for an answer but maybe im trying to be too specific.所以我环顾四周寻找答案,但也许我试图过于具体。

this is the jquery code im using :这是我使用的 jquery 代码:

$(document).ready(function(){

    $(".slidingDiv").hide();
    //hides the div 

    $(".show_hide").show();
    //click on the img to show hidden div 

    $('.show_hide').click(function(){
    $(".slidingDiv").slideToggle("slow");

});


heres example of the html :

<div class">


<img height="300" width="350" src="images/web.png" alt="design 1"  class=" show_hide img-responsive center-block wow fadeIn" href="#"/>
<br>

</div>  

when i click this image , it toggles the hidden div :

<div class="slidingDiv">


          <div>


      <img height="300" width="350" src="images/web.png" alt="design 1"  class="img-responsive center-block" />

          </div>
</div>

css: 

.slidingDiv{
height:400px;
background-color:black;
padding:50px;
margin-top:10px;
margin-bottom: 35px;
border-bottom:1px solid #3399FF;
}

.show_hide{
display:none;
}

and this works awesome when i add "show_hide" class to the image i want to use , then the div appears nicely above when i add the class "slidingDiv" , but how do i implement this into more than one image with different divs with jquery?当我将“show_hide”类添加到我想要使用的图像时,这非常有效,然后当我添加类“slidingDiv”时,div 会很好地显示在上方,但是我如何使用 jquery 将其实现到多个具有不同 div 的图像中?

i just registered today at stack so sory if im doing somethings wrong.我今天刚刚在堆栈注册,如果我做错了什么。

Here is how you can do,这是你可以做的,

$(document).ready(function(){

    $(".slidingDiv").hide();
    //hides the div 

    $(".show_hide").click(function(){
        $(".slidingDiv").show("slow"); 
    });


});

Here is a demo这是一个演示

If you want to make multiple div appear and disappear when you click on a single image you can add the class slidingDiv to those div如果要在单击单个图像时使多个 div 出现和消失,可以将类slidingDiv添加到这些 div

<div class="slidingDiv">
    text text text text text text text
    text text text text text text text
    text text text text text text text
    text text text text text text text
    text text text text text text text
    text text text text text text text
    text text text text text text text
    text text text text text text text
</div>
<div class="slidingDiv">
    text text text text text text text
    text text text text text text text
</div>
<div class="slidingDiv">
    text text text text text text text
    text text text text text text text
</div>

Demo for multiple images多张图片演示

With the HTML setup you have, you will want to navigate up and then to the sibling .slidingDiv like使用您拥有的HTML设置,您将需要向上导航,然后导航到同级.slidingDiv

$('.show_hide').click(function() {
        $(this).closest('div').next(".slidingDiv").slideToggle("slow");
      });

 $(document).ready(function() { $(".slidingDiv").hide(); //hides the div $(".show_hide").show(); //click on the img to show hidden div $('.show_hide').click(function() { $(this).closest('div').next(".slidingDiv").slideToggle("slow"); }); });
 .slidingDiv { height: 75px; background-color: black; padding: 50px; margin-top: 10px; margin-bottom: 35px; border-bottom: 1px solid #3399FF; } .show_hide { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div> <img height="30" width="35" src="images/web.png" alt="design 1" class=" show_hide img-responsive center-block wow fadeIn" href="#" /> <br> </div> <div class="slidingDiv"> <div> <img height="30" width="35" src="images/web.png" alt="design 1" class="img-responsive center-block" /> </div> </div> <div> <img height="30" width="35" src="images/web.png" alt="design 1" class=" show_hide img-responsive center-block wow fadeIn" href="#" /> <br> </div> <div class="slidingDiv"> <div> <img height="30" width="35" src="images/web.png" alt="design 1" class="img-responsive center-block" /> </div> </div>

Although you seem to have found your solution but to provide you with another alternative, ie if you are interested in the first place, here is what I have been able to produce .虽然您似乎已经找到了您的解决方案,但为您提供了另一种选择,即如果您首先感兴趣,这是我能够制作的 This is an overly simplistic example mind you and the intention is to provide you with an idea.请注意,这是一个过于简单的示例,目的是为您提供一个想法。

The way it works is that it would get the index() of clicked element out of elements that are click-able and it would apply this index() on the set of other elements that need to be animated with the help of eq() method.它的工作方式是从可点击的元素中获取被点击元素的index()并将此index()应用于需要借助eq()进行动画处理的其他元素集方法。

JavaScript: JavaScript:

$(document).ready(function(){
    $('.image-container').on('click',function(){
        $('.sliding-div').eq($(this).index()).stop().slideToggle();
    });
});

Benefit of using this approach is that you don't have to repeat code.使用这种方法的好处是您不必重复代码。 Another benefit is that your sliding-div elements do not necessarily have to remain close to image-container elements in HTML, they can be wherever they need to be in the markup.另一个好处是,您的sliding-div元素不必与 HTML 中的image-container元素保持接近,它们可以位于标记中需要的任何位置。 But the disadvantage is that it is strictly dependant on the order with which HTML markup has been laid out.但缺点是它严格依赖于 HTML 标记的布局顺序。 So, for example, if second element out of image-container elements is clicked, the second element out of sliding-div elements will be animated.因此,例如,如果单击image-container元素中的第二个元素,则sliding-div元素中的第二个元素将被动画化。

Hope this helps in some way.希望这在某种程度上有所帮助。

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

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