【问题标题】:jQuery hide div on click and show another divjQuery 在点击时隐藏 div 并显示另一个 div
【发布时间】:2016-11-22 20:25:09
【问题描述】:

我正在玩 jQuery,我试图在单击父 div 时显示一个隐藏的 div。单击父 div 后,将在其中隐藏一些 span 标签并显示隐藏 div 中的 p 标签。这一点我可以按照下面的小提琴开始工作。

$(document).ready(function() {
  $('.wrapper').click(function() {
    $('.hidden-text').toggle();
    $('.test-span').hide();
  });
});
.wrapper {
  border: 5px solid #718373;
  width: 200px;
  height: 200px;
  position: relative;
  cursor: pointer;
}
.hidden-text {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <h4>Heading</h4>
  <span class="test-span">some text</span>
  <span class="test-span">some more text</span>
  <div class="hidden-text">
    <p>A half an hour lesson for one person will include 25 clays and 25 cartridges</p>
  </div>
</div>

我遇到的问题是,我想再次“点击”来扭转上述情况,但我只是不确定如何去做。我仍在学习如何使用 jQuery。

【问题讨论】:

    标签: jquery html css


    【解决方案1】:

    您可以通过同时在两个元素上调用toggle() 来实现此目的。当它们以相反的状态开始时,切换将简单地切换它们中的哪一个在任何给定时间可见。试试这个:

    $(document).ready(function() {
      $('.wrapper').click(function() {
        $('.hidden-text, .test-span').toggle();
      });
    });
    .wrapper {
      border: 5px solid #718373;
      width: 200px;
      height: 200px;
      position: relative;
      cursor: pointer;
    }
    .hidden-text {
      display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="wrapper">
      <h4>Heading</h4>
      <span class="test-span">some text</span>
      <span class="test-span">some more text</span>
      <div class="hidden-text">
        <p>A half an hour lesson for one person will include 25 clays and 25 cartridges</p>
      </div>
    </div>

    【讨论】:

    • 有点晚了,但没问题,很高兴为您提供帮助! :)
    猜你喜欢
    • 2017-04-19
    • 2015-10-15
    • 2016-11-27
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多