简体   繁体   English

单击具有特定类或ID的div时显示和隐藏内容

[英]Displaying and hiding content when div with a certain class or id is clicked

I have a 4 divs that are stacked on top of each other. 我有一个4 div,它们彼此叠放。 When I click on a given div, I want it to show a content section below it and above the other three divs. 当我单击一个给定的div时,我希望它在其下方和其他三个div上方显示一个内容部分。 When I click the div again, it should hide the content section associated with it. 当我再次单击div时,它应该隐藏与其关联的内容部分。 My current code is able to display the content on click but I can't find anything on how to hide that content when I click on the div again. 我当前的代码可以在单击时显示内容,但是当我再次单击div时,找不到关于如何隐藏该内容的任何信息。

Here is my code for example 这是我的代码

HTML 的HTML

                <div class="dropItem wow fadeInLeft">Nature <b class="caret"></b></div>
                <div id="show">
                    <p>Some text here</p>
                </div>

                <div class="dropItem2 wow fadeInLeft">Discovery <b class="caret"></b></div>
                <div id="show2">
                    <p>Some text here</p>
                </div>

                <div class="dropItem3 wow fadeInLeft">Perspective <b class="caret"></b></div>
                <div id="show3">
                    <p>Some text here</p>
                </div>

                <div class="dropItem4 wow fadeInLeft">Mindfullness <b class="caret"></b></div>
                <div id="show4">
                    <p>Some text here</p>
                </div>

            </div>
        </div><!--end about column-->

Javascript Java脚本

$(document).ready(function(){
    //hide content sections at first
    $("#show").hide();
    $("#show2").hide();
    $("#show3").hide();
   $("#show4").hide();

 //display each content section when corresponding div is clicked
    $(".dropItem").click(function(){
        $("#show").show();
    });

    $(".dropItem2").click(function(){
       $("#show2").show();
    });

    $(".dropItem3").click(function(){
        $("#show3").show();
     });

     $(".dropItem4").click(function(){
         $("#show4").show();
      });

});

CSS 的CSS

.dropItem, .dropItem2, .dropItem3, .dropItem4 {
 width: 100%;
 height: 25%;
 padding-top: 4%;
 text-align: center;
 border-top: 1px solid #000;
 font-weight: bold;
}

.caret {
 color: teal;
}

#show, #show2, #show3, #show4 {
 width: 100%;
 height: 250px;
 background-color: teal;
 color: #FFFFFF;
 border: 1px solid teal;
}

Link to site for example http://jackloudphoto.com/ 链接到网站,例如http://jackloudphoto.com/

You can do this with this three solutions. 您可以使用这三种解决方案来完成此操作。

First of all, good idea is wrap it into one div as box, because you need define item borders. 首先,好主意是将其包装到一个div作为框中,因为您需要定义项目边框。


Solution one 解决方案一

I like this because no matter how much deep is content div inside in box. 我之所以这样,是因为无论div盒中的内容div有多深。 If you change box structure eg. 如果您更改框结构,例如。 you don't need to change your js code because everything will work. 您无需更改js代码,因为一切都会正常进行。

HTML 的HTML

<!-- solution one -->
<div class="box">
  <div class="box-header dropItem wow fadeInLeft">Nature <b class="caret"></b>
  <div class="box-content">
    <p>Some text here</p>
  </div>
</div>

JS JS

// solution one
$('.box .box-header').click(function(e) {
    $(this).closest('.box').find('.box-content').toggle();
});


Solution two 解决方案二

It is similar to your code. 它类似于您的代码。 You need get ID what you want to show. 您需要获取要显示的ID。 For this is perfect use HTML5 Data attribute like in example. 为此,可以像示例中一样使用HTML5 Data属性。 But if you need to change your structure, you probably will need change js code too. 但是,如果您需要更改结构,则可能还需要更改js代码。

HTML 的HTML

<!-- solution two -->
<div class="box2">
  <div class="box-header2 dropItem wow fadeInLeft" data-box-id="2">Nature <b class="caret"></b>
  <div class="box-content2 item-2">
    <p>Some text here 222</p>
  </div>
</div>

JS JS

// solution two
$('.box2 .box-header2').click(function(e) {
    var id = $(this).data('box-id');
  $('.item-'+id).toggle();
});


Solution three 解决方法三

Is solution one without short toggle function. 是没有短切换功能的解决方案之一。

HTML 的HTML

<!-- solution three -->
<div class="box3">
  <div class="box-header3 dropItem wow fadeInLeft">Nature <b class="caret"></b>
  <div class="box-content3 item-3">
    <p>Some text here 3333</p>
  </div>
</div>

JS JS

$('.box3 .box-header3').click(function(e) {
    var $content = $(this).closest('.box3').find('.box-content3');
  if($content.is(':hidden')) {
    $content.show();
  }
  else {
    $content.hide();
  }
});

I prefer the solution one but sometimes use solution two. 我更喜欢一种解决方案,但有时使用两种解决方案。 Depend what is frendly for you. 取决于对您有利的事物。

JS DEMO JS演示

Fast and easy answer: 快速简便的答案:

var one = -1,
    two = -1,
    three = -1,
    four = -1;


     //hide content sections at first
     $("#show").hide();
     $("#show2").hide();
     $("#show3").hide();
     $("#show4").hide();

     //display each content section when corresponding div is clicked
     $(".dropItem").click(function(){
         if(one == -1)
         $("#show").show();
         else
          $("#show").hide();

          one *= -1;
     });

     $(".dropItem2").click(function(){
        if(two == -1)
         $("#show2").show();
         else
          $("#show2").hide();

          two *= -1;
     });

     $(".dropItem3").click(function(){
         if(three == -1)
         $("#show3").show();
         else
          $("#show3").hide();

          three *= -1;
     });

     $(".dropItem4").click(function(){
         if(four == -1)
         $("#show4").show();
         else
          $("#show4").hide();

          four *= -1;
     });

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

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