jQuery .click() .toggle() - change display and swap character

后端 未结 2 1308
清歌不尽
清歌不尽 2021-01-22 16:02

Here is my jQuery:

$(\'.ask\').click(function() {
    $(\'.addtitle\').slideToggle(\'fast\', function() {
    // Animation complete.
    });
});
<
相关标签:
2条回答
  • 2021-01-22 16:15
    $('.ask').click(function() {
      var $this = $(this);
    
      $this.find('.addtitle').slideToggle('fast', function() {
         // Animation complete.
         if($this.text() === '-')
            $this.text('+');
         else
            $this.text('-');
      });
    });
    
    0 讨论(0)
  • 2021-01-22 16:21

    You can do it using .next() and .toggle() like this:

    $('.ask').toggle(function() {
      $(this).text('-').next('.addtitle').slideDown('fast');
    }, function() {
      $(this).text('+').next('.addtitle').slideUp('fast');
    });
    

    .next(selector) get the next sibling if it matches the selector, if there may be something in-between then use .nextAll('.addtitle:first'), .toggle() cycles between the functions you provide on each click event, so it'll swap the text and slide appropriately every click.

    0 讨论(0)
提交回复
热议问题