开发者

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

Here is my jQuery:

$('.ask').click(function() {
    $('.addtitle').slideToggle('fast', function() {
    // Animation complete.
    });
});

And my HTML:

                            <p class="ask">+</p>
                                <div class="addtitle">
                                    <p>Give your Idea a great title</p>
                                    <form name="input" actio开发者_C百科n="#" method="get">
                                        <input id="title" type="text" name="title" />
                                        <input id="go" type="submit" value="Go" />
                                    </form> 
                                </div>

I have multiple .ask div's on the page and I only want it to effect the .next() div named .ask rather than all div's named .ask.

I also want to .toggle() the '+' character to '-'

Can anyone assist?


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.


$('.ask').click(function() {
  var $this = $(this);

  $this.find('.addtitle').slideToggle('fast', function() {
     // Animation complete.
     if($this.text() === '-')
        $this.text('+');
     else
        $this.text('-');
  });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜