Change div text with jQuery Toggle

后端 未结 8 1066
粉色の甜心
粉色の甜心 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:52

    Use this

    jQuery.fn.toggleText = function() {
        var altText = this.data("alt-text");
        if (altText) {
            this.data("alt-text", this.html());
            this.html(altText);
        }
    };
    

    Here is how you use it

     
       jQuery.fn.toggleText = function() {
        	var altText = this.data("alt-text");
    
        	if (altText) {
        		this.data("alt-text", this.html());
        		this.html(altText);
        	}
        };
    
        $('[data-toggle="offcanvas"]').click(function ()  {
    
        	$(this).toggleText();
        });
    
    

    You can even use html provided it's html encoded properly

提交回复
热议问题