jQuery Toggle Text?

后端 未结 20 954
日久生厌
日久生厌 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:12

    In most cases you would have more complex behavior tied to your click event. For example a link that toggles visibility of some element, in which case you would want to swap link text from "Show Details" to "Hide Details" in addition to other behavior. In that case this would be a preferred solution:

    $.fn.extend({
      toggleText: function (a, b){
        if (this.text() == a){ this.text(b); }
        else { this.text(a) }
      }
    );
    

    You could use it this way:

    $(document).on('click', '.toggle-details', function(e){
      e.preventDefault();
      //other things happening
      $(this).toggleText("Show Details", "Hide Details");
    });
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-12-02 06:16

    I've written my own little extension for toggleText. It may come in handy.

    Fiddle: https://jsfiddle.net/b5u14L5o/

    jQuery Extension:

    jQuery.fn.extend({
        toggleText: function(stateOne, stateTwo) {
            return this.each(function() {
                stateTwo = stateTwo || '';
                $(this).text() !== stateTwo && stateOne ? $(this).text(stateTwo)
                                                        : $(this).text(stateOne);
            });  
        }
    });
    

    Usage:

    ...
    <button>Unknown</button>
    ...
    //------- BEGIN e.g. 1 -------
    //Initial button text is: 'Unknown'
    $('button').on('click', function() {
        $(this).toggleText('Show', 'Hide'); // Hide, Show, Hide ... and so on.
    });
    //------- END e.g. 1 -------
    
    //------- BEGIN e.g. 2 -------
    //Initial button text is: 'Unknown'
    $('button').on('click', function() {
        $(this).toggleText('Unknown', 'Hide'); // Hide, Unknown, Hide ...
    });
    //------- END e.g. 2 -------
    
    //------- BEGIN e.g. 3 -------
    //Initial button text is: 'Unknown'
    $('button').on('click', function() {
        $(this).toggleText(); // Unknown, Unknown, Unknown ...
    });
    //------- END e.g.3 -------
    
    //------- BEGIN e.g.4 -------
    //Initial button text is: 'Unknown'
    $('button').on('click', function() {
        $(this).toggleText('Show'); // '', Show, '' ...
    });
    //------- END e.g.4 -------
    
    0 讨论(0)
  • 2020-12-02 06:17

    Use html() to toggle HTML content. Similar to fflyer05's code:

    $.fn.extend({
        toggleText:function(a,b){
            if(this.html()==a){this.html(b)}
            else{this.html(a)}
        }
    });
    

    Usage:

    <a href="#" onclick='$(this).toggleText("<strong>I got toggled!</strong>","<u>Toggle me again!</u>")'><i>Toggle me!</i></a>
    

    Fiddle: http://jsfiddle.net/DmppM/

    0 讨论(0)
  • 2020-12-02 06:19

    Improving and Simplifying @Nate's answer:

    jQuery.fn.extend({
        toggleText: function (a, b){
            var that = this;
                if (that.text() != a && that.text() != b){
                    that.text(a);
                }
                else
                if (that.text() == a){
                    that.text(b);
                }
                else
                if (that.text() == b){
                    that.text(a);
                }
            return this;
        }
    });
    

    Use as:

    $("#YourElementId").toggleText('After', 'Before');
    
    0 讨论(0)
  • 2020-12-02 06:19
    var el  = $('#someSelector');    
    el.text(el.text() == 'view more' ? 'view less' : 'view more');
    
    0 讨论(0)
提交回复
热议问题