简体   繁体   English

响应按钮单击显示和隐藏文本

[英]Showing and hiding text in response to a button click

The code below does this.下面的代码就是这样做的。 When I click the button for the first time, it shows the respective text.当我第一次单击按钮时,它会显示相应的文本。 And when I click the same button again, it fades out and fades in. However, I want the respective text to disappear if the button is clicked the second time instead of fading out and then fading in.当我再次单击同一个按钮时,它会淡出并淡入。但是,如果第二次单击该按钮,我希望相应的文本消失,而不是淡出然后淡入。

$(document).ready(function(){
    $('.info').click(function(){
        $(this).next().fadeOut("slow");
    $(this).next().fadeIn("slow");
    });
});

HTML: HTML:

    <div>
        <a class="info" p style="font-size:30px" href="javascript:void(0);">header 1</a>
        <h1 class="infoText" style="display:none">text 1</h1>
    </div>
    <div>
        <a class="info" href="javascript:void(0);">header 2</a>
        <h1 class="infoText" style="display:none">text 2</h1>
    </div>
    <div>
        <a class="info" href="javascript:void(0);">header 3</a>
        <h1 class="infoText" style="display:none">text 3</h1>
    </div>

It should start like this:它应该像这样开始:

header1标题1

header2标题2

header3标题3

When I click header1, it should be like this:当我单击 header1 时,它应该是这样的:

header1标题1

text1文本1

header2标题2

header3标题3

And when I click header 1 again, it should be like this:当我再次单击标题 1 时,它应该是这样的:

header1标题1

header2标题2

header3标题3

Anybody know how to do this?有人知道怎么做吗?

You can use fadeToggle() to toggle the display with fade effect您可以使用fadeToggle()使用淡入淡出效果切换显示

 $(document).ready(function() { $('.info').click(function() { $(this).next().fadeToggle("slow"); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <a class="info" p style="font-size:30px" href="javascript:void(0);">header 1</a> <h1 class="infoText" style="display:none">text 1</h1> </div> <div> <a class="info" href="javascript:void(0);">header 2</a> <h1 class="infoText" style="display:none">text 2</h1> </div> <div> <a class="info" href="javascript:void(0);">header 3</a> <h1 class="infoText" style="display:none">text 3</h1> </div>

You can use toggle() for this purpose.为此,您可以使用 toggle()。 It will hide the element if it is already shown, otherwise it will shows the element.如果元素已经显示,它将隐藏该元素,否则它将显示该元素。

$('.info').click(function(){
     $(this).next().toggle("slow");
 });
$('.info').click(function(){
    //$(this).next().fadeOut("slow");
    $(this).next().toggle("slow");
});

Use toggle to hide and show something使用切换来隐藏和显示某些东西

AS per suggestion of @Tushar根据@Tushar 的建议

$('.info').click(function(){
    //$(this).next().fadeOut("slow");
    $(this).next().fadeToggle("slow");
});

You can use toggle() Function:您可以使用 toggle() 函数:

$(document).ready(function() {
  $('.info').click(function() {
    $(this).next().fadeToggle("slow");
  });
});

DEMO演示

You Try this code你试试这个代码

$(document).ready(function() {
  $('.info').click(function() {
      var _self = $(this);

      if(_self.hasClass("active")) {
          _self.next().fadeOut("slow");
         _self.removeClass("active");
      } else {
           _self.next().fadeIn("slow");         
          _self.addClass("active");
      }

  });
});

Working Demo工作演示

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

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