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

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

Here is my jQuery:

$(\'.ask\').click(function() {
    $(\'.addtitle\').slideToggle(\'fast\', function() {
    // Animation complete.
    });
});
<
2条回答
  •  再見小時候
    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.

提交回复
热议问题