jQuery Toggle Text?

后端 未结 20 959
日久生厌
日久生厌 2020-12-02 05:52

How to toggle HTML text of an anchor tag using jQuery? I want an anchor that when clicked the text alternates between Show Background & Show Text

20条回答
  •  时光取名叫无心
    2020-12-02 06:14

    jQuery.fn.extend({
            toggleText: function (a, b){
                var isClicked = false;
                var that = this;
                this.click(function (){
                    if (isClicked) { that.text(a); isClicked = false; }
                    else { that.text(b); isClicked = true; }
                });
                return this;
            }
        });
    
    $('#someElement').toggleText("hello", "goodbye");
    

    Extension for JQuery that only does toggling of text.

    JSFiddle: http://jsfiddle.net/NKuhV/

提交回复
热议问题