Change div text with jQuery Toggle

后端 未结 8 786
粉色の甜心
粉色の甜心 2021-02-01 04:07

When using slideToggle, how to change the Text close/show? I did a simple one, but cannot get the text change back.

Here is what I did:

8条回答
  •  一整个雨季
    2021-02-01 04:40

    You can use the is() assertion method to check whether the panel is open or closed in the animation's callback and set the text accordingly - http://jsfiddle.net/9EFNK/7/

    $('.open').click(function(){
        var link = $(this);
        $('.showpanel').slideToggle('slow', function() {
            if ($(this).is(':visible')) {
                 link.text('close');                
            } else {
                 link.text('open');                
            }        
        });       
    });
    

提交回复
热议问题