Jquery添加类显示块而没有

时间:2015-01-07 02:55:34

标签: jquery css

大家好,每个人都可以在我的代码中帮助我。

$(document).ready(function() {
     var circle = $('.circle');
     $(".send a").click(function(e) {
      e.preventDefault();
     $('.wrap').css('display', 'block');

        if (circle.hasClass('bounceInLeft')) {
         circle.removeClass('bounceInLeft').addClass('bounceOutRight');

          }
           else 
          {

         $('.circle').addClass('animated bounceOutRight');
         circle.removeClass('bounceOutRight').addClass('bounceInLeft');
           }

      });

  });

点击.send a后,.wrapdisplay:block。如何添加第二次点击.send a,然后.wrap display none?我知道它应该是这样的:

$('.wrap').css('display', 'none');但我不知道在哪里可以写这段代码......

.wrap {
  max-width: 400px;
  margin: 4em auto;
  text-align: center;
  display:none;
  background-color:#fff;
}

2 个答案:

答案 0 :(得分:1)

您可以使用.toggle()切换每次点击时元素的展示次数。

$(".send a").on('click', function(e) {
    $('.wrap').toggle();
    // ..
});

或者,最好使用.toggleClass(),然后添加/删除.hide等类:

.hide {
    display: none; /* You may wish to increase the specificity.. */
}
$(".send a").on('click', function(e) {
    $('.wrap').toggleClass('hide');
    // ..
});

如果您坚持手动添加/删除内联CSS,则只需使用条件语句,如:

$('.wrap').is(':hidden') ? $('.wrap').show() : $('.wrap').hide();

答案 1 :(得分:0)

Demo

我添加的是一个变量,它可以找出您点击锚元素的次数。

这种方式非常简单,我希望它对你有用。祝你好运!

相关问题